Syntax error when passing method argument in link_to in rails - ruby-on-rails

When I put :method => :delete in link_to I'm getting syntax error and I don't understand why.
Here's the code:
<%= link_to application_path(application), :method => :delete, :confirm => 'Are you sure?', :title => 'Delete task', :class => "color-red" do %>
<span class="glyphicon glyphicon-trash">
<% end %>

Related

Adding a class to link_to

I have been trying to add a class to my link_to for over an hour now. No matter what I try, I get an error.
<% if user_signed_in? %>
<li>
<%= link_to('Sign out', destroy_user_session_path, :method => :delete) %>
</li>
<% else %>
<li>
<%= link_to('Sign in', new_user_session_path) %>
</li>
<% end %>
I am trying to add :class => "page-scroll btn-signin" to both of my link_to lines.
I solved the problem
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete, :class => "page-scroll btn-signin" %>
This code works correctly.
<%= link_to 'Sign in', new_user_session_path, :class => "page-scroll btn-signin" %>
and
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete, :class => "page-scroll btn-signin" %>
More info
EDIT
Destructive actions should be performed as a form submission - link
use button_to on Sign Out.
<%= button_to "Sign out", destroy_user_session_path, :method=>:delete, :class => "page-scroll btn-signin" %>

NameError in index.html.erb

I am having an issue getting my layout to render at localhost:3000/skills. Here is my view code:
This is the index.html.erb view:
<% if #skills.blank? %>
<p>There are not any skills currently saved in the system.</p>
<% else %>
<p>These are the current skills saved in our system</p>
<ul id="skills">
<% #skills.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new Skill", {:action => 'new' }%></p>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b>
<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this skill??" %></b>
</li>
this is my error:
NameError in Skills#index
Showing /app/views/skills/index.html.erb where line #16 raised:
undefined local variable or method `c' for #<#<Class:0x000000055b1620>:0x0000000413d338>
and the specific line it is pointing to is:
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
I think the c.id is an invalid param for my database but I am not sure. I will keep trying to fix it and thanks anyone who knows anything. Cheers and good luck!
<% if #skills.blank? %>
<p>There are not any skills currently saved in the system.</p>
<% else %>
<p>These are the current skills saved in our system</p>
<ul id="skills">
<% #skills.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %> <- **c ends here**
</ul>
<% end %>
<p><%= link_to "Add new Skill", {:action => 'new' }%></p>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b>
<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this skill??" %></b>
</li>
You have to move <% end %> after you end using it. I suppose it is after the last
<% if #skills.blank? %>
<p>There are not any skills currently saved in the system.</p>
<% else %>
<p>These are the current skills saved in our system</p>
<ul id="skills">
<% #skills.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b>
<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this skill??" %></b>
</li>
<% end %>
</ul>
<p><%= link_to "Add new Skill", {:action => 'new' }%></p>
<% end %>

Ruby in Rails, destroy does not work in edit. routing to show

I have a problem with the destroy action.
It works in the index.html.erb very fine.
But I can't get it work in the edit.html.erb
It routes to the show even without asking for a permission.
Many solutions here say it has something to do with jQuery. Well as you can see, I tried all I could find.
<%= stylesheet_link_tag "application" %>
<!--Delete Problem in Edit-->
<%#= javascript_include_tag :all %>
<%#= javascript_include_tag :application %>
<%#= javascript_include_tag :default %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Is there a solution that works for my situation?
This is my delete button in edit:
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
<%= t "list.button_delete" %>
<%#= link_to I18n.t(".list.delete"), contact, confirm: 'Are you sure?', method: :delete %>
<%#= link_to 'delete', contact_path(#contact), :method => :delete %>
<% end %>
I tried many things, as well.
The following is the action:
def destroy
##contact = Contact.find(params[:id])
#contact = current_user.contacts.find(params[:id])
#contact.destroy
redirect_to contacts_url,
alert: 'Successfully deleted the contact'
end
Replace
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
with
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", data: {confirm: 'Are you sure?'}, data: {method: :delete}) do %>
It should be data: {confirm: 'Are you sure?'} instead of confirm: 'Are you sure?' and also data: {method: :delete} instead of method: :delete .
where, link_to method interpolates confirm: 'Are you sure?' as data-confirm="Are you sure?" and method: :delete as data-method="delete".
BUT content_tag method interpolates confirm: 'Are you sure?' as confirm="Are you sure?" and method: :delete as method="delete" so your javascript call is not getting invoked.
In your destroy action replace:
#contact = current_user.contacts.find(params[:id])
with:
#contact = #current_user.contacts.find(params[:id])

Adding a class to link_to block

I have a following code which displays a 'delete' link:
<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.
You need to add the URL as the first parameter, then the html options, e.g.:
<%= link_to resource_path(#resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
I actually found this to be a working solution with Rails 4.2
<%= link_to(resource_path(#resource), class: "project-card clearfix") do %>
<h1>Your html here</h1>
<% end %>
If you need to pass a controller and action, like edit and destroy, do it as follow:
<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
<i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
<i class="material-icons">close</i>
<% end %>
The link_to docs:
link_to(body, url, html_options = {})
So you'd want
<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Add a value to a delete link in Rails?

I currently have the following link_to in my Rails application:
<%= link_to 'Remove this Person', #person, :confirm => 'Are you sure?', :method => :delete, :class => 'important', :class => "minimal important" %>
I would like to add the person's name in place of the "this person" for the link.
I don't appear to be able to do:
<%= link_to 'Remove <%= #person.name %>', #person, :confirm => 'Are you sure?', :method => :delete, :class => 'important', :class => "minimal important" %>
I have seen this example online:
<%= link_to(#profile) do %>
<strong><%= #profile.name %></strong> -- <span>Check it out!</span>
<% end %>
# => <a href="/profiles/1">
<strong>David</strong> -- <span>Check it out!</span>
</a>
Any ideas on how to convert that into a delete link, with the confirmation box?
Try this.
<%= link_to "Remove #{#person.name}", #person, :confirm => 'Are you sure?', :method => :delete, :class => 'important', :class => "minimal important" %>

Resources