-2

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  %>
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • Welcome to stackoverflow. Please format your code and also try to remove unneccessary code, most of it is not needed to answer the question. http://stackoverflow.com/help/mcve – Eyeslandic Apr 19 '17 at 08:21

1 Answers1

0

You should change this

user_company_path(@user, @company)

to

user_company_path(@user, company)

in show.html.erb.

References: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to https://stackoverflow.com/a/1549211/1776169

Community
  • 1
  • 1
yatindra rao
  • 107
  • 7