Well i have an application in Rails 5. My problem is that i have nested resources for example user/1/companies/1
all the companies that every user owns are listed in the show action of the user. Everything works perfect until the point where i want that every user click in their company name (using the helper link_to) but all the companies are redirect to the first company that the user created
routes.rb
Rails.application.routes.draw do
devise_for :admins
devise_for :users
resources :users do
resources :companies do
resources :parts
end
end
root 'welcome#index'
end
userscontroller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@company = @user.companies.find_by(params[:company_id])
@companies = current_user.companies.all.order('created_at DESC') if current_user
end
end
show.html.erb
<% @companies.each do |company|%>
<tbody>
<tr>
<th scope="row"><%=company.id %></th>
<td><%=link_to company.name, user_company_path(@user, @company) %></td>
<td><%= company.description %></td>
<td>@mdo</td>
</tr>
</tbody>
<% end %>