Link_to remote => true, causing strange routing problems, Rails 3

I am having problems with link_to remotely sending a message to another controller ... The result is not quite what I expect.

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 %> 

And I have this in video_controller:

  def iframize @video = Video.find(params[:video_id]) respond_to do |format| format.js end end 

And this is in the routes:

  resource :videos do collection do post 'iframize' end end 

The problem is that when I click the link, it takes me to

 http://localhost:3000/videos/iframize?method=post&video_id=20 

and i get

 Couldn't find Video with id=iframize 

I looked through dozens of different examples, and they seem to recommend the above, but that doesn't work. What am I doing wrong?

Any entry is welcome!

Thanks!

EDIT:

I tried this function of the jQuery approach, and it seemed to work (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}); } }); 
0
source share
2 answers

You have rails_ujs enabled through jquery_ujs in the assets / application.css file

 = link_to image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg"), "url_path", :method => :post, :remote => true 

Convert the following to a rails path: e.g. iframeize_videos_path (generated using rake routes )

 :controller => 'videos', :action => "iframize", :video_id => vid.id, 
0
source

You need to distinguish URL parameters, use html options.so

 <%= 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} %> 
0
source

Source: https://habr.com/ru/post/912270/


All Articles