Search code examples
ruby-on-railsurlrouteslink-to

Easy Rails link_to


I'm iterating through a collection of results, and I have this link_to in my code:

<%= link_to "", "#{expo.id}/edit" %>

I expected it go to <domain>/exhibitions/3/edit. I'm already in the /exhibitions page so that's where I got that from. The problem is, is that it goes to <domain>/exhibitions/3 and ends there. For some reason it doesn't tag on the edit portion of the URL. Can anyone help? Thank you!

UPDATE

The code is wrapped in this:

<% @exhibition.each do |expo| %>
    <%= link_to "", expo_edit_path(:id) %>
<% end %>

UPDATE 2

I've run rake routes and I did have the incorrect edit path. Now the problem is the URL. The URL literally becomes: http://localhost:3000/exhibitions/id/edit It literally writes out the word id.

This is my new link_to statement: <%= link_to "", edit_exhibition_path(:id) %>

The error is as follows:

ActiveRecord::RecordNotFound in ExhibitionsController#edit Couldn't find Exhibition with 'id'=id


Solution

  • Use something like

    link_to "text", edit_expo_path(expo)
    

    You should be use a route as defined in config/routes.rb - read up on rails routing to see the format.

    You may find https://stackoverflow.com/a/12141379/631619 helpful.
    Also https://stackoverflow.com/a/29419670/631619