adding a method and html options to link_to in rails? - ruby-on-rails

I'm brand new to ruby and rails and I'm having trouble creating a sign-out link (using devise for auth). I want to pass a custom :method parameter into the link_to function, and set a custom class. I seem to be able to do one or the other but not both.
When I try:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete, { :class => 'signout'} %>
I get the proper result from clicking the link, but I lose my styling. On the other hand, when I try:
<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout'}, :method => :delete %>
I get the styling I want but the link request is passed as GET rather than DELETE, resulting in a routing error.
What am I missing?

Try placing both :class and :method inside the hash.
So:
<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout', :method => :delete} %>

You don't need hash here, because it is already hash :D
link_to "Sign out", destroy_user_session_path, method: "delete", class: "signout"
profit!

Related

Incorrect symbols: Devise returns syntax error when coloring text on buttons

I am configuring an upload form for a crowdfunding site.
The following code works. It gives me white text on the Edit Profile button:
<button class="btn btn-success large"><%= link_to 'Edit profile', edit_user_registration_path, {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%></button>
But when I try and add colors to the following line, Devise returns a syntax error:
<button class="btn btn-success large"><%= link_to "Logout", destroy_user_session_path, method: :delete {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%></button>
The problem is incorrect syntax/symbols between the words :delete and {:style. I have followed the error messages and tried every combination of symbols , : => ( and { but none are correct.
I'm making a syntax error, but am not sure what. Thanks if you can help.
Try this
<%= link_to "Logout", destroy_user_session_path, method: :delete,:style=>"color:#FFFFFF;", :class => "navbar-link css-class" %>
You have syntax error:
Replace
<%= link_to "Logout", destroy_user_session_path, method: :delete {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%>
with
<%= link_to "Logout", destroy_user_session_path, method: :delete, {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%>

Using link_to on a list element in rails

I'm trying to making the final list element a clickable link to my search_path in rails using HAML but have been unable to make it work.
Here is what I have so far:
%li
= link_to('sign out', destroy_user_session_path, :method => :delete)
%li
= link_to "profile", edit_user_registration_path
%li{:style => 'background-color:#3D8599;'}
%i.fa.fa-search
Everything I have tried results in the fa-search icon being put in the wrong place or not being clickable.
Have you tried this?
%li{:style => 'background-color:#3D8599;'}
= link_to search_path do
%i.fa.fa-search

How to make :confirm in link_to working with I18n.t() in Rails 3.2?

The following :confirm pops up a window asking 'Delete the record?':
<%= link_to t('Delete'), misc_definition_path(#misc_definition), :method => :delete, :confirm => 'Delete the record?'
If adding I18n.t() to the message, however the confirm window does not pop up:
<%= link_to t('Delete'), misc_definition_path(#misc_definition), :method => :delete, :confirm => I18n.t('Delete the record?')
I tried "#{I18n.t()}" and not working. Is there a way the message can be I18n.t?
The syntax is correct.
Try this -
Add some key in your config/locales/*.yml
confirm_delete: 'Delete Record'
Use this key as-
<%= link_to t('Delete'), misc_definition_path(#misc_definition), :method => :delete, :confirm => I18n.t('confirm_delete') %>

rails delete user session path

I am using the devise gem to make authentication work in my app.
Here's the code I have for signing out:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
I have tried this as well:
<%= link_to "Sign out", destroy_user_session_path%>
Both of which when I click on sign out I get :
No route matches [GET] "/users/destroy"
However when I run rake routes, you can see it (just not GET):
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
How to fix this?
Mitch's answer was close but did not work for me, instead the following syntax did:
<%= link_to "Log out", destroy_user_session_path, :method => :delete %>
Could this help you in the right direction?
https://github.com/plataformatec/devise/issues/1195
Roger's link above is v useful.
I used the following syntax, which worked:
<%= link_to "Log out", destroy_session_path(:user), :method => :delete %>

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

Resources