Search code examples
ruby-on-railsruby-on-rails-4viewlink-to

link_to under show view


How can I convert these links and make them work in the show view?

I use the below button set in the index view but wanting to move these links under show view. I tried variations and different ways but can't get them to work.

The user has nested addresses and business profiles, I'm also trying to link over to edit and delete.

<%= link_to 'Edit User', edit_users_main_path(users_main), class: "btn" %>
<%= link_to 'User Address', users_main_contacts_path(users_main), class: "btn" %>
<%= link_to 'User Business', users_main_businesses_path(users_main), class: "btn" %>
<%= link_to 'Delete User', users_main, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn" %>

Solution

  • I am not sure that what you are expecting in users_main, however, I assume it should be an user object. You need to define it in show action show that you can use it in respective view.

    # Controller
    def show
      @user = User.find(params[:user_id]) # just an example to define your instance varibale
      ...
      ...
    end
    

    Then use same instance variable in the view

    # View
    ...
    ...
    
    <%= link_to 'Edit User', edit_users_main_path(@user), class: "btn" %>
    <%= link_to 'User Address', users_main_contacts_path(@user), class: "btn" %>
    <%= link_to 'User Business', users_main_businesses_path(@user), class: "btn" %>
    <%= link_to 'Delete User', @user, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn" %>
    
    ...