How can I pass an extra parameter to a link_to remote? - ruby-on-rails

Given the following:
<%= link_to "Add", group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>
<%= link_to "Ignore", group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>
How can I pass a decision param with either add or ignore in the link_to's above? Is there an option to add a URL param somehow with a link_to? I don't want to have to use forms.

the *_path helpers accept a hash of options to append to the path as a query string.
In your example, assuming group_request_path(gr) outputs something like /group_requests/123, you could add a query string with add/ignore actions like this:
group_request_path(gr, :action => 'add') # /groups_requests/123?action=add
or
group_request_path(gr, :add => 1) # /groups_requests/123?add=1
However this probably isn't the correct way of going about this. It looks like you should probably have distinct actions in your controller, and routes specific to adding or ignoring. You'll have to add something like the following to config/routes.rb:
put 'group_requests/:id/add' => 'groups_requests#add', :as => :add_group_request
put 'group_requests/:id/ignore' => 'groups_requests#ignore', :as => :ignore_group_request
Your links then become
<%= link_to "Add", add_group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>
<%= link_to "Ignore", ignore_group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>

Related

Rails link_to for destroy action: How to pass parameters, data confirmation action and instyle css

<%= link_to "[Delete]", customer_path(customer.id, :customer_delete => true), :method => :delete, :data => {:confirm => "Are You Sure?"} %>
My above code working fine,
But how to give my link_to a color say ike red in my view ? Tried many stack overflow threads but couldn't find a solution as my link_to contains a combine functionality like data confirmation action with passing parameters
To give color property to Link
1. new link with css class
<%= link_to "[Delete]", customer_path(customer.id, :customer_delete => true), :method => :delete, :data => {:confirm => "Are You Sure?"}, :class => 'link' %>
application.css
.link {
color: red;
}
2. new link with inline css style
<%= link_to "[Delete]", customer_path(customer.id, :customer_delete => true), :method => :delete, :data => {:confirm => "Are You Sure?"}, :style => 'color: red;' %>

Wrong controller action in rails form with link

I have a form...
= simple_form_for #form, :url => update_path, :method => :put do |form|
= form.input_field :name
= link_to image_tag asset_path("icon.png"),
{:controller => 'controller/name', :action => 'delete', :id => some.id}
= form.submit
As in above form I have a link/image/icon, which on click should fire delete action controller, but for some reason it goes to index controller action. Can someone point what's wrong here please?
When you define a resources routes, the delete action will be defined as the same url as the update action (which is accessed using PUT or PATCH HTTP verbs),
but it is accessed using the DELETE HTTP verb
All you need to do is set the link to the update path of your object and use the DELETE verb instead:
= link_to image_tag("icon.png"), update_path(id: some.id), method: :delete
Also, no need for #asset_path, I think
Try doing:
= link_to image_tag asset_path("icon.png"),
{:controller => 'controller_name', :action => 'delete_method_name', :id => some.id},
{:confirm => 'Are you sure?', :method => :delete}
Issue is that, you need to pass :method as :delete, :action should be the method name of your controller not the HTTP method.

link_to using PUT method syntax to a different controller in rails 3

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

Rails link_to destroy a nested resource?

I have a nested resource attachments and I want to create a link_to to destroy/delete the attachment. Here's what I have, but it is posting as a GET versus a PUT:
<%= link_to "Delete Attachment", project_thread_attachment_path(#attachment.thread.project.id, #attachment.thread.id, #attachment.id), :confirm => "Are you sure you want to delete this attachment?", :method => :delete, :action => "destroy" %>
Ideas? Thanks!
Try
link_to "Delete Attachment", [#attachment.thread.project,#attachment.thread,#attachment], :confirm => "Are you sure?", :method => :delete
Does it work?
You should be able to use the following by itself (remove the :action => 'destroy' part). Also, the request should be a DELETE request, not a PUT request:
<%= link_to "Delete Attachment", project_thread_attachment_path(#attachment.thread.project.id, #attachment.thread.id, #attachment.id), :confirm => "Are you sure you want to delete this attachment?", :method => :delete %>

Why I can't change the link_to become link_to remote directly?

I have a link_to like this:
<%= link_to 'X', category, :confirm => 'Are you sure?', :method => :delete %>
So, I change it like this:
<%=link_to_remote category.name,
:confirm => 'Are you sure?', :method => :delete%>
it show the confirm box, but after I click "yes", it won't delete the item I want , why?
There is no information about what url you need to go to in your link_to_remote usage. You should also specify something like
:url => delete_category_path(category)
in the options hash.

Resources