I am having some problems with link_to remote sending a post to another controller... The result are 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 videos_controller:
def iframize
#video = Video.find(params[:video_id])
respond_to do |format|
format.js
end
end
And this in routes:
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
and I get
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?
Any input greatly appreciated!
Thanks!
EDIT:
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});
}
});
Do you have rails_ujs included via jquery_ujs in 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 into a rails path: like iframeize_videos_path (generated via rake routes)
:controller => 'videos', :action => "iframize", :video_id => vid.id,
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} %>
Related
I'm sure that this has something to do with my N00b syntax but I'm having trouble working out what it is...
I am creating a menu by looping through items in my subpages table and creating a link for each item that is returned, Like this:
<% #subpages.each do |menu| %>
<%= link_to(menu.name, {:controller => 'public', :action => "page", :id => menu.permalink }, :class => "show action footer-link") %>
<% end %>
this is working fine on the homepage of my site but if you visit one of the subpages, let's say the about us page:
http://localhost:3000/public/page/about-us
and then try to use the menu again to visit "contact us", instead of taking you to this link as i would expect:
http://localhost:3000/public/page/contact-us
It takes you to this link:
http://localhost:3000/public/page/about-us?id=contact-us
What school-boy error am I making here?
Thanks in advance.
edit: my routes
root :to => "public#index"
get 'admin', :to => 'access#menu'
get 'public/show/:permalink', :to => 'public#show'
get 'public/page/:permalink', :to => 'public#page'
Try with this :
<% #subpages.each do |menu| %>
<%= link_to(menu.name, page_public_path(menu.permalink), :class => "show action footer-link") %>
<% end %>
Thanks
I managed to get it to work by passing the :permalink param to the controller instead of the ID
<% #subpages.each do |menu| %>
<%= link_to(menu.name, { :action => "page", :permalink => menu.permalink }, :class => "show action footer-link") %>
<% end %>
I try to call an action of my Gallery controller from a Portfolio view. A Portfolio is made of many galleries.
I try this:
<%= link_to("Heart", gallery_path(gallery), :action => "like", :method => :put , :remote => true) %>
<%= link_to("Heart", :controller => :galleries, :action => "like", :method => :put , :remote => true) %>
And I obtain:
Heart
and
Heart
I want to get but i m stuck...:
Heart
Any RAILS God to help me ?
I believe you're getting behaviour because you're trying to use the "path helper" and "params hash" styles in the same link_to (see the docs for more details). I prefer the path helper style, so I'd write the link like this:
<%= link_to(
'Heart',
like_gallery_path(gallery),
{:method => :put, :remote => true}
) %>
If you like the params hash style, you'd write:
<%= link_to(
'Heart',
{:controller => 'galleries', :action => 'like', :id => gallery.id},
{:method => :put, :remote => true}
) %>
Note that the URL parameters (controller, action, etc.) are in a separate hash from the link parameters (method & remote).
Hope that helps!
Try with:
<%= link_to "Heart", gallery_path(gallery), :url => { :controller => "galleries", :action => "like"}, :method => :put, :remote => true) %>
i was wondering why does
<%= form_tag( { :action => "/search", :method => "get" }, :class => "span4" ) do %>
...
<% end %>
give the following error?
No route matches [POST] "/assets"
i notice it's because of the /search. if i rewrote the code as...
<%= form_tag( { :action => "search", :method => "get" }, :class => "span4" ) do %>
...
<% end %>
without the /search, it correctly calls my controller method. can someone explain why? thanks
This will do what you mean:
<%= form_tag( "/search", :method => "get" , :class => "span4" ) do %>
...
<% end %>
If the first parameter of form_tag is a hash as you have given, it is passed behind-the-scenes to url_for, which inteprets :action as the action part of a route for it to reverse-map.
Since you (I guess) want to just pass a plain URL, just pass it as a string for the first arg.
More info here of course :)
the ":action" should receive a name of "action", such as: "create", "update", or "destroy"
:action => "/search"
here the "/search" is not a name, but an "url", which is not legal.
btw, I suggest you use xx_url instead of { :action => "", :controller => ""}, e.g.
<%= form_tag "/search", :method => "get" %>
or
# you defined "search_path" in config/routes.rb
<%= form_tag search_path, :method => "get" %>
I have the following button :
<%= button_to 'Add to Cart', line_items_path(:product_id => product),
:remote => true %>
I want to replace it by a link_to containing an image with text on it.
I am ok with the HTML CSS part, but i want the request to be for line_items#create not for line_items#index
How can i do that?
Try this:
<%= link_to "Add to Cart", {:controller => "line_items", :action => :create}, :remote => true %>
And don't forget to update routes.rb too, e.g.:
get "/blablabla", :to => "line_items#create"
After a bit of try and error i found that :
<%= link_to ("<div>Ajouter au panier</div>"+image_tag("some.jpg")).html_safe,
line_items_path(:product_id => #product),
:action => :create,
:remote => true,:method => :post%>
It works perfectly fine!
Something is seriously not adding up here.. My page just refreshes, nothing happens, it never touches any of my debuggers hanging out on all my methods except for index.
my html:
<%- for image in #images %>
<%= image.attachment_file_name %>
<%-# link_to_delete image, :url => destroy_image_admin_wysiwyg_path(image.id) %>
<%= link_to 'delete', { :url => destroy_image_image_path(image.id) },
#:confirm => 'Are you sure?',
:post => true
%>
<br />
<% end %>
my controller
def destroy_image
debugger
#img = Image.find(params[:id])
#img.destroy
respond_to do |format|
format.html { redirect_to admin_image_rotator_path }
end
end
My routes:
map.resources :images, :member => { :destroy_image => :post }
My disgusting hack that works that I will replace as soon as I find something better
I moved the action over to a simpler controller I built myself.
Changed my routes to :
admin.resources :wysiwygs, :member => { :destroy_image => :post }
Changed my html :
<%= link_to 'delete', :controller => "wysiwygs", :action => "destroy_image" %>
But when I clicked on the link..it brought up.. the show action ?? fffffffffuuuuuuu
I retaliated by just moving my action to the show action, and passing a hidden field in my html..
<%= link_to 'delete', :controller => "wysiwygs", :action => "destroy_image", :hidden_field => {:value => image.id} %>
def show
# this was previously in destroy_image
#img = Image.find(params[:hidden_field][:value])
#img.destroy
respond_to do |format|
format.html { redirect_to admin_image_rotator_path }
end
end
It seems you're going down the wrong path here. If a before_filter is blocking your action, figure out why. Use skip_before_filter :filter_name if the filter is not needed.
Don't use show actions or HTTP GET for deletes. Even if it works, it will confuse things down the road. Use a DELETE verb:
map.resources :images, :member => { :destroy_image => :delete }
pass it in the link helper:
<%= link_to "delete", destroy_image_image_path(image), :method => :delete %>
And use ImagesController#destroy_image to perform the action. Better yet, consider using the standard RESTful ImagesController#destroy which map.resources gives you for free.
Not sure what was wrong in the first place, but in your second, working solution, i think you should write your link_to as follows:
link_to 'delete', :controller => "wysiwygs", :action => "destroy_image", :id => image.id
That at least would send you to the correct action.
Depending on your routes, you will have to make this a method => :post or not.
Check your rake routes output, it will show you what are the possible routes, and also what names they got, which in turn you can use as a method (add _path or _url at the end). Then it should be even easier to write something like:
link_to 'delete', wysiwygs_destroy_image_path(image)
Good luck!
You're doing a POST but your resource says that :destroy_image is only available via GET. Try changing your route to:
map.resources :images, :member => { :destroy_image => :post }
Also, take a look at your link_to. The second parameter takes a URL, not a hash that has a :url key. As mentioned elsewhere, depending on your Rails version you may need :method => :post instead of :post => true. In Rails 2.3.8, you would want this line instead:
<%= link_to 'delete', destroy_image_image_path(image), :method => :post %>