I am trying to get a link_to to display a partial via jquery ajax, but can't seem to get it to work (edit: returning blank screen) and I'm not sure what I'm missing. Any help would be appreciated.
我试图得到一个link_to来显示部分通过jquery ajax,但似乎无法让它工作(编辑:返回空白屏幕),我不知道我错过了什么。任何帮助,将不胜感激。
I would like to click the link "Preview Widget" and have it display _widget.html.erb in the div preview.
我想点击“预览窗口小部件”链接,让它在div预览中显示_widget.html.erb。
From my understanding the link "Preview Widget" should call the action def preview_widget which calls preview_widget.js.erb which then renders the partial _widget.html.erb in the div.
根据我的理解,链接“预览小部件”应该调用动作def preview_widget,它调用preview_widget.js.erb然后在div中呈现部分_widget.html.erb。
EDIT: updates link as per Ignatius Reza suggestions
编辑:根据Ignatius Reza的建议更新链接
show.html.erb
show.html.erb
<%= link_to "Preview Widget", :action => 'preview_widget' , :id => @widget.id, :remote => true %> %>
<div id="preview"></div>
widget_controller.rb
widget_controller.rb
def preview_widget
respond_to do | format |
format.js {render :layout => false}
end
end
preview_widget.js.erb
preview_widget.js.erb
$( "#preview" ).html( "<%= escape_javascript( render( :partial => "widget", :locals => { :widget => @widget} ) ) %>" );
_widget.html.erb
_widget.html.erb
<% @widget.videos.each do |video| %>
<h3><a href='#'><%= video.name %></a></h3>
<div>
<object height='316' width='540'>
<embed style='border: 1px solid #333333;' height='316' width='540' allowfullscreen='true' allowscriptaccess='always' type='application/x-shockwave-flash' src='<%= video.url %>'>
</object>
</div>
<% end %>
routes.rb
的routes.rb
match 'preview_widget' => 'widgets#preview_widget'
3 个解决方案
#1
6
Ok finally got this to work with the following changes.
routes.rb (added member to widget resources)
好的,最后得到了以下更改。 routes.rb(添加成员到窗口小部件资源)
resources :widgets do
member do
get 'preview_widget'
end
end
show.html.erb (changed link_to to match routes)
show.html.erb(更改了link_to以匹配路由)
<%= link_to 'Preview', preview_widget_widget_path(:id => @widget.id), :remote => true %>
This now shows the partial. I am still not 100% sure what was wrong with the previous code - at least it now works.
现在显示部分。我仍然不能100%确定以前的代码有什么问题 - 至少它现在有效。
#2
5
It's not cleared on your question, what you "can't seem to get it to work".. but from what i see in the code you gave.. all seems right, but you miss the actual ajax call..
在你的问题上没有清除,你“似乎无法让它工作”......但是从我给你的代码中看到的......一切似乎都是对的,但是你错过了真正的ajax调用..
it could be added by adding :remote => true to the "Preview Widget" link such as :
它可以通过添加:remote => true添加到“预览窗口小部件”链接,例如:
<%= link_to "Preview Widget", :action => 'preview_widget' , :id => @widget, :remote => true %>
if the default behaviour is enough.. or you could add your own custom ajax call on application.js..
如果默认行为足够..或者您可以在application.js上添加自己的自定义ajax调用..
as a note, i don't think setting the :id attribute of "Preview Widget" link to @widget is wise.. as it will put the string representation of widget, which usually will looks like "<Widget:0x12412 ..>" perhaps it would be better to change it to "widget-link-#{@widget.id}"
作为一个注释,我不认为设置“预览窗口小部件”链接到@widget的:id属性是明智的..因为它将放置窗口小部件的字符串表示,通常看起来像“
#3
4
I’ve a similar problem that brought me to this threat, so I thought sharing my solution here might be helpful to anybody.
我有一个类似的问题让我受到这种威胁,所以我认为在这里分享我的解决方案可能对任何人都有帮助。
With :remote => true
I got an normal HTTP Request to http://localhost:3000/info/about?remote=true
instead of the wanted AJAX Request to http://localhost:3000/info/about
使用:remote => true我得到一个正常的HTTP请求到http:// localhost:3000 / info / about?remote = true而不是想要的AJAX请求到http:// localhost:3000 / info / about
The fix was easy but hard to find!
修复很容易,但很难找到!
In my HAML View:
在我的HAML视图中:
WRONG Code that triggers HTTP Request
错误触发HTTP请求的代码
= link_to( image_tag("icons/information.png", :title => t('menu.info')), :controller => "info", :action => "about", :remote => true )
OK-Code that triggers AJAX Request
OK-Code触发AJAX请求
= link_to( image_tag("icons/information.png", :title => t('menu.info')), {:controller => "info", :action => "about"}, :remote => true )
The only difference is {curly brackets}!
唯一的区别是{花括号}!
Funny though is that with the AJAX Request I get info/about.html
rendered, without the layout file. Which isn’t a partial but is close to what Jan wanted. I was expecting info/about.js.erb
to be rendered.
有趣的是,使用AJAX请求我得到info / about.html呈现,没有布局文件。这不是偏袒的,但接近于Jan想要的。我期待渲染info / about.js.erb。
In InfoController
在InfoController中
def about
respond_to do |format|
format.html # renders ‘views/info/about.html.erb’ inside layout template on HTTP Request
format.js# renders ‘views/info/about.html.erb’, without layout
end
end
-
-
def about
respond_to do |format|
format.html # => ‘views/info/about.html.erb’ inside layout template on HTTP Request
format.js {render 'about.js'} # => renders ‘views/info/about.js.erb’
end
end
#1
6
Ok finally got this to work with the following changes.
routes.rb (added member to widget resources)
好的,最后得到了以下更改。 routes.rb(添加成员到窗口小部件资源)
resources :widgets do
member do
get 'preview_widget'
end
end
show.html.erb (changed link_to to match routes)
show.html.erb(更改了link_to以匹配路由)
<%= link_to 'Preview', preview_widget_widget_path(:id => @widget.id), :remote => true %>
This now shows the partial. I am still not 100% sure what was wrong with the previous code - at least it now works.
现在显示部分。我仍然不能100%确定以前的代码有什么问题 - 至少它现在有效。
#2
5
It's not cleared on your question, what you "can't seem to get it to work".. but from what i see in the code you gave.. all seems right, but you miss the actual ajax call..
在你的问题上没有清除,你“似乎无法让它工作”......但是从我给你的代码中看到的......一切似乎都是对的,但是你错过了真正的ajax调用..
it could be added by adding :remote => true to the "Preview Widget" link such as :
它可以通过添加:remote => true添加到“预览窗口小部件”链接,例如:
<%= link_to "Preview Widget", :action => 'preview_widget' , :id => @widget, :remote => true %>
if the default behaviour is enough.. or you could add your own custom ajax call on application.js..
如果默认行为足够..或者您可以在application.js上添加自己的自定义ajax调用..
as a note, i don't think setting the :id attribute of "Preview Widget" link to @widget is wise.. as it will put the string representation of widget, which usually will looks like "<Widget:0x12412 ..>" perhaps it would be better to change it to "widget-link-#{@widget.id}"
作为一个注释,我不认为设置“预览窗口小部件”链接到@widget的:id属性是明智的..因为它将放置窗口小部件的字符串表示,通常看起来像“
#3
4
I’ve a similar problem that brought me to this threat, so I thought sharing my solution here might be helpful to anybody.
我有一个类似的问题让我受到这种威胁,所以我认为在这里分享我的解决方案可能对任何人都有帮助。
With :remote => true
I got an normal HTTP Request to http://localhost:3000/info/about?remote=true
instead of the wanted AJAX Request to http://localhost:3000/info/about
使用:remote => true我得到一个正常的HTTP请求到http:// localhost:3000 / info / about?remote = true而不是想要的AJAX请求到http:// localhost:3000 / info / about
The fix was easy but hard to find!
修复很容易,但很难找到!
In my HAML View:
在我的HAML视图中:
WRONG Code that triggers HTTP Request
错误触发HTTP请求的代码
= link_to( image_tag("icons/information.png", :title => t('menu.info')), :controller => "info", :action => "about", :remote => true )
OK-Code that triggers AJAX Request
OK-Code触发AJAX请求
= link_to( image_tag("icons/information.png", :title => t('menu.info')), {:controller => "info", :action => "about"}, :remote => true )
The only difference is {curly brackets}!
唯一的区别是{花括号}!
Funny though is that with the AJAX Request I get info/about.html
rendered, without the layout file. Which isn’t a partial but is close to what Jan wanted. I was expecting info/about.js.erb
to be rendered.
有趣的是,使用AJAX请求我得到info / about.html呈现,没有布局文件。这不是偏袒的,但接近于Jan想要的。我期待渲染info / about.js.erb。
In InfoController
在InfoController中
def about
respond_to do |format|
format.html # renders ‘views/info/about.html.erb’ inside layout template on HTTP Request
format.js# renders ‘views/info/about.html.erb’, without layout
end
end
-
-
def about
respond_to do |format|
format.html # => ‘views/info/about.html.erb’ inside layout template on HTTP Request
format.js {render 'about.js'} # => renders ‘views/info/about.js.erb’
end
end