Link_to remote =>真正导致奇怪的路由问题,Rails 3 [英] Link_to remote => true causing strange routing problems, Rails 3
问题描述
我在使用 link_to 远程向另一个控制器发送帖子时遇到了一些问题...结果与我预期的不太一样......
I am having some problems with link_to remote sending a post to another controller... The result are not quite what I expect..
我在 node_content.html.erb 中有这个:
I have this in node_content.html.erb:
<% @node.videos.each do |vid| %>
<div id="vid_vid"><%= link_to image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg"), :controller => 'videos', :action => "iframize", :video_id => vid.id, :method => :post, :remote => true %></div>
<% end %>
我在videos_controller中有这个:
And I have this in videos_controller:
def iframize
@video = Video.find(params[:video_id])
respond_to do |format|
format.js
end
end
这在路由中:
resource :videos do
collection do
post 'iframize'
end
end
问题是当我点击链接时,它把我带到
Problem is that when I click the link, it takes me to
http://localhost:3000/videos/iframize?method=post&video_id=20
我得到
Couldn't find Video with id=iframize
我看了几十个不同的例子,他们似乎推荐了上面的,但它不起作用..我做错了什么?
I looked through tens of various examples and they seem to recommend the above, but it does not work.. What am I doing wrong?
非常感谢任何输入!
谢谢!
我尝试了这种方法 jquery 函数,它有点工作(当然仅适用于循环中的第一个视频):
I tried this approach jquery function and it kinda worked (only for the first video in the loop of course):
<% @node.videos.each do |vid| %>
<%= image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg", :id => 'img_div') %>
<div id="vid_vid"> <%= vid.id %></div>
<% end %>
$('#img_div').on({
'click': function() {
var vid_id = document.getElementById("vid_vid").innerHTML;
$.post("/videos/iframize/", {video_id: vid_id});
}
});
推荐答案
需要区分url选项,html选项,所以使用
You need to differentiate the url options, html options.so use
<%= link_to image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg"),
{ :controller => 'videos', :action => "iframize", :video_id => vid.id },
{ :method => :post, :remote => true} %>
这篇关于Link_to remote =>真正导致奇怪的路由问题,Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!