我有两个模型乔布斯和问题。工作有很多问题和问题属于工作。 我已经在模型中设置了资源以及路径。我在尝试在问题#index页面上链接问题控制器的Show方法时遇到问题。我的rake路由说路径应该是job_question_path,有两个必要的:id是:job_id和:id,所以我试过:
<td><%= link_to 'Show', job_question_path(@job, question) %></td>
并收到错误:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil, :id=>#<Question id: 1, job_id: 1, question1: "sfsdfssfs", question2: "sfsdfs", question3: "sfsdf", question4: "sfsdfsf", question5: "sfsfsfs", created_at: "2011-06-21 03:25:12", updated_at: "2011-06-21 03:25:12">}
我尝试了多个其他组合,似乎没有任何工作,我一直在努力:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil }
或其中某种组合。
我没有得到的部分是我可以放入url / jobs / 1 / questions / 1并将它带到显示页面,所以我假设我的问题#show方法没问题。请参阅下面的其余代码。
问题#index视图
<% @questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<td><%= link_to 'Show', job_question_path(@job, question) %></td>
</tr>
&lt;%end%&gt;
问题控制器
def index
@questions = Question.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @questions }
end
end
def show
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @question }
end
end
模型
class Job < ActiveRecord::Base
has_many :questions
class Question < ActiveRecord::Base
belongs_to :job
的routes.rb
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
有关我的佣金路线,请参阅此https://gist.github.com/1032734。
感谢您提前提供任何帮助,我已经在这一段时间了,并且无法找到解决方案。如果您需要更多信息,请与我们联系。
答案 0 :(得分:3)
可能是这样吗?
问题#index视图
<% @questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<%= link_to 'Show', job_question_path(question.job_id, question.id) %>
</tr>
它必须工作。或者,你没有“job_id&#39;问题表中的字段?