开发者

How do I link a different controller id variable 'car' to a parent header/link subject?

I am trying to make it so that when you click the corresponding subject you are taken to the correct sub section which is on a different controller. Basically the subject controller/model is something like the Mercedes Benz, I want it so when you click that su开发者_如何学编程bject you are taken to a page on 'car' controller to view active posts or discussions related to that subject.

subject controller class:

def index
  @subject = Subject.all
  @car = Car.find(params[:id])
end


end

subject index:

<h2>Choose Your Car Model</h2>
<dl>
<% @subject.each do |subject| %>
  <dd>
    <a href="<%=car.name %>"><%= subject.name %></a><br />
  </dd>
<% end %>
</dl>

error message when viewing subject index:

ActiveRecord::RecordNotFound in SubjectController#index

Couldn't find Car without an ID


Generally the index method does not receive the :id parameter, but the show method does. If you're using nested resources, you may be looking for the :car_id parameter instead:

def index
  @subject = Subject.all
  @car = Car.find(params[:car_id])
end

Always check your log/development.log to see what the contents of params is as well as checking that the route is defined properly with rake routes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜