0

Imagine this. I'm on the URL http://localhost:3000/companies/3, and on this view, I want an employees button that takes me to http://localhost:3000/companies/3/employees

In my routes file, I have

  namespace :companies, :only => [:index, :show, :new, :edit, :destroy], :path => "/:companies" do
    resources :employees, :only => [:index, :edit, :new, :show] do
    end
  end

I understand that I need to link to the index action of 'employees', but I don't know what to add to my companies 'show' page. Right now, it looks like

%a{title: "company1", href: "#"} Employees

(I am using haml, but feel free to post your answer in erb)

In rake routes, I get

companies_employees GET    /:companies/employees(.:format)          companies/employees#index

... but I want companies/company_id/employees

Thanks!

1 Answer 1

1

link to employees in the company show page

<%= link_to 'Employees', company_employees_path(@company) %>

employees controller

def index
  @company = Company.find(params[:company_id])
  @employees = @company.employees
end

routes

  resources :companies do
    resources :employees
  end

rake routes

company_employees GET /companies/:company_id/employees(.:format) employees#index

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.