如何链接不同的控制器 id 变量“car”?到父标题/链接主题?
我试图做到这一点,以便当您单击相应的主题时,您将被带到位于不同控制器上的正确子部分。基本上,主题控制器/模型类似于梅赛德斯奔驰,我想要它,因此当您单击该主题时,您将被带到“汽车”控制器上的页面,以查看与该主题相关的活动帖子或讨论。
主题控制器类:
def index
@subject = Subject.all
@car = Car.find(params[:id])
end
end
主题索引:
<h2>Choose Your Car Model</h2>
<dl>
<% @subject.each do |subject| %>
<dd>
<a href="<%=car.name %>"><%= subject.name %></a><br />
</dd>
<% end %>
</dl>
查看主题索引时出现错误消息:
ActiveRecord::RecordNotFound in SubjectController#index
Couldn't find Car without an ID
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 subject 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,
index
方法不会接收:id
参数,但show
方法会接收。如果您使用嵌套资源,则可能需要查找:car_id
参数:始终检查
log/development.log
以查看的内容params
以及检查是否使用rake paths
正确定义了路由。Generally the
index
method does not receive the:id
parameter, but theshow
method does. If you're using nested resources, you may be looking for the:car_id
parameter instead:Always check your
log/development.log
to see what the contents ofparams
is as well as checking that the route is defined properly withrake routes
.