Rails, destroy link NoMethodError - ruby-on-rails

I have the following list of items:
= #kid.educations.each do |education|
= education.studies_centre
= _('-')
= education.city_and_country
= link_to _("<i class='fa fa-times-circle a-lg'></i> delete").html_safe, education, :confirm => 'Are you sure?',:method => :delete, class: "btn btn-danger btn-xs pull-right"
%br
%small
= education.academic_qualification
%hr
%br
As you can see, I have a delete option, but I does not work. I get:
undefined method `education_path' for #<#<Class:0x007fd00c61c270>:0x007fd00cdd5b88>
What I'm doing wrong.
Thanks for your help
UPDATE ROUTES
dashboard_kid_educations GET /dashboard/kids/:kid_id/educations(.:format) dashboard/educations#index
POST /dashboard/kids/:kid_id/educations(.:format) dashboard/educations#create
new_dashboard_kid_education GET /dashboard/kids/:kid_id/educations/new(.:format) dashboard/educations#new
edit_dashboard_kid_education GET /dashboard/kids/:kid_id/educations/:id/edit(.:format) dashboard/educations#edit
dashboard_kid_education GET /dashboard/kids/:kid_id/educations/:id(.:format) dashboard/educations#show
PUT /dashboard/kids/:kid_id/educations/:id(.:format) dashboard/educations#update
DELETE /dashboard/kids/:kid_id/educations/:id(.:format) dashboard/educations#destroy

You're using namespaced nested resources, so your link should be like this:
= link_to _("<i class='fa fa-times-circle a-lg'></i> delete").html_safe, [:dashboard, #kid, education], :confirm => 'Are you sure?',:method => :delete, class: "btn btn-danger btn-xs pull-right"

If you using nested resources you should pass 2 arguments to path helper. And I think it's better to use block to improve code readability
= link_to dashboard_kid_education_path(#kid, education), confirm: 'Are you sure?', method: :delete, class: 'btn btn-danger btn-xs pull-right' do
%i.fa.fa-times-circle.a-lg
= 'delete'

Nested Routes
Something to add to Marek's answer is the idea of nested routes
Essentially, when you define a nested route (like below), Rails does not instinctively know when you're calling this:
#config/routes.rb
resources :dashboard do
resources :education
end
The bottom line is that Rails will only call routes as they pertain to the model of the object you're calling. In your example, you call education (which is presumably built from Eduction.find())
To Rails, by passing this to your routes, you will essentially tell your application to find routes for the Education model only, hence why your error looks like this:
undefined method `education_path'
--
The way to fix this, as detailed by Marek is to ensure you are referencing the correct path in your <%= link_to %> call. To do this, you have to reference the nested route as described by Marek's solution

Related

Methods with link_to In ruby tags (ruby on rails)

I am working on a Ruby on Rails project, and I noticed that the link_to can either work with or without a method specified.
With the method specified:
<%= link_to "Log In", new_user_session_path, class: 'btn btn-primary navbar btn', method: :get %>
Without the method specified:
<%= link_to "About", about_path, class: 'navbar-brand' %>
How do I know when should I use a method with link_to, and when I should not?
From my understanding, you should use method when it's different from get. Eg, I specify it when I need to use post and so on.
It tells the browser which HTTP method to use when submitting the request to the web server. Supported verbs are :post, :get, :delete, :patch, and :put.
You can always specify it if you want to be explicit, otherwise it's only necessary when you need it to deviate from the default. Different functions have different defaults.
button_to defaults to :post
link_to defaults to :get
etc.
This link has many examples and a more thorough explanation:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

Understanding Haml Code

I am currently working with a RoR project where my team is using Haml as a templating engine. I have difficulty understanding the following code.
%td
= link_to 'Edit', [:edit, :merchants, category, subcategory], class: 'btn'
= link_to 'Delete', [:merchants, category, subcategory], :method => :delete, :data => { :confirm => 'Are you sure?' }, class: 'btn'
Which translate into html as
<a class="btn" href="/admin/categories/28/sub_categories/147/edit">Edit</a>
<a class="btn" data-confirm="Are you sure?" data-method="delete" href="/admin/categories/28/sub_categories/147" rel="nofollow">Delete</a>
From Haml documentation, I found out that [] notation used for object reference so I am not sure how the [ ] notation translated into href notation. I am pretty new to both Ruby On Rails and Haml, so any help will be appreciated.
It nothing about the Haml. It is a Rails helper what's builds some_awesome_path from arrays of :symbol and variables.
Internally, Rails will convert [:edit, :merchants, category, subcategory] to a call to merchants_category_subcategory_path. Most (if not all) Rails methods that expect a path will also take an object representation of your resource, like respond_with, link_to, render, redirect_to, form_for, etc.
Some example:
This array:
[:new, #object, :post]
Translate to:
new_businesses_post_path() and new_businesses_post_url()
Creating URLs From Objects
In Rails we have two ways to build pathes
by *_path methods
by polymorphic pathes http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html#method-i-polymorphic_url
Also some methods (form_for, link_to) not requires to call polymorpic_path and able to take array directly.

The difference between delete and the other routes

Below I have the link-helpers for the actions edit and destroy. The first link (and all the others) is working perfectly but the second creates a weird url that doesn't work.
<%= link_to "Edit", edit_event_path(organizer_vanity_url: event.organizer.vanity_url, id: event.id) %>
<%= link_to 'Remove', event_path(organizer_vanity_url: event.organizer.vanity_url, id: event.id), method: :delete, data: { confirm: 'Are you sure?' } %>
This is from the routes.rb:
scope "organizer" do
scope ":organizer_vanity_url" do
scope "manage" do
resources :events
end
end
end
What is the difference between the delete link-helper and the others (as that's the only one that doesn't work)?
link_to - is GET-request-like helper (by default)
DELETE method is POST-like method
so, you passing post method to get helper and receive "weird url"
to solve this you have two options:
use button_to instead of link_to helper (first one is for post form submitting, by default)
use js to correctly handle your link.

Add css class to rails link_to helper

I'm trying to style a rails link using css using the following code:
<%= link_to "Learn More", :controller => "menus", :action => "index", :class => "btn btn-inverse" %>
I would expect that this would create a link that looks like this:
Learn More
Instead, rails is rendering this -
Learn More
Has anyone else had this problem / know what I'm doing wrong? I know I can avoid this problem by manually creating the anchor tag rather than using helper, but I was wondering if there was a way to pass the css class info to the helper itself. I'm using Rails 3.2.6.
Thanks!
You have a syntax problem. Try this instead:
<%= link_to "Learn More", {controller: "menus", action: "index"}, class: "btn btn-inverse" %>
Some documentation for you to go further with the link_to Helper
They say:
Be careful when using the older argument style, as an extra literal hash is needed:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => Articles
Leaving the hash off gives the wrong link:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => WRONG!
I recommend you to use the URL helper generated following your routes configuration. In your case:
link_to "Learn More", menus_path, :class => "btn btn-inverse"
A little reminder on the Helpers generated:
# routes.rb
resources :users
# any view/controller
users_path #=> /users
edit_user_path(user) #=> /users/:id/edit
user_path(user) #=> /users/:id (show action)
new_user_path(user) #=> /users/new
Try new argument convention:
<%= link_to 'Learn More', 'menus#index', class: 'btn btn-inverse' %>
if you do not have a controller action / route necessary for the link, you can pass nil as the placeholder and get the classes to apply as necessary
<%= link_to 'link verbiage', nil, class: 'classes for action tag'%>
I solved my problem by the way
<%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>
This is how i solved it using another view engine, HAML just in case a fellow developer is having this need
%i= link_to "Add New Blog Post", user_post_edit_new_url(current_user), :class => "fa fa-plus-circle"

Making a delete confirmation page using Ruby on Rails 3

I am trying to add a new page to my RoR3 application that should display a delete confirmation of a user account. It should match the 'destroy' action in 'ROOT_RAILS/controllers/accounts_controller.rb'.
At this time my problem occurs on creating a "link_to" this page, but maybe I am wrong somewhere and my work is not completed yet.
So, what I made, is:
I created the 'ROOT_RAILS/views/accouns/delete.html.erb' file.
I updated the routes.rb like this:
resources :accounts do
collection do
get 'delete'
post 'delete'
end
end
I don't know the next steps, but now if I try to insert this code
<%= link_to 'Delete', delete_account_path(#current_account) %>
in my views, I will get this error:
undefined method `delete_account_path' for #<#<Class:0x00...>
What I have to do?
This "link_to" works, but, of course, doesn't make what I would like:
<%= link_to 'Delete', delete_users_accounts_path %>
Try the following:
config/routes.rb:
resources :accounts do
get :delete, :on => :member
end
In the view before the delete page:
<%= link_to 'Delete', delete_account_path(#current_account) %>
In the delete view(this will invoke the destroy method in your controller):
<%= link_to 'Delete', #current_account, :confirm => "Are you sure?", :method => :delete %>

Resources