Domanda

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!

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top