嵌套资源的 Link_to 路由问题
我有两个模型“作业”和“问题”。一份工作有很多问题,而问题又属于一份工作。 我已经在模型中设置了资源以及路线。我在尝试 link_to questions#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 }
或其中的某种组合。
我没有得到的部分是,我可以输入网址 /jobs/1/questions/1 ,它会将我带到显示页面,所以我假设我的 questions#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>
<% end %>
问题 控制器
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 了解我的 rake 路线。
感谢您提前提供的任何帮助,我已经从事此工作有一段时间了,只是无法找出解决方案。如果您需要更多信息,请告诉我。
I have two models Jobs and Questions. A job has many questions and questions belong to a job.
I've set up the resources in the model, as well as the routes. I am having an issue trying to link_to the Show method of the questions controller on the questions#index page. My rake routes say that the path should be job_question_path with the two necessary :id's being :job_id and :id , so I tried:
<td><%= link_to 'Show', job_question_path(@job, question) %></td>
and got the error:
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">}
I've tried multiple other combos and nothing is seeming to work, I keep getting:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil }
or some combination of that.
The part I don't get is that I can put in the url /jobs/1/questions/1 and it takes me to the show page, so I am assuming that my questions#show methods are ok. See below for the rest of my code.
Questions#index view
<% @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>
<% end %>
Questions Controller
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
Models
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"
See this https://gist.github.com/1032734 for my rake routes.
Thanks for any help in advance, i've been at this for a while now and just can't figure out the solution. Please let me know if you need any more info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是这样?
Questions#index view
它必须有效。或者问题表中没有“job_id”字段吗?
may be so?
Questions#index view
It have to work. Or haven't you 'job_id' field in Questions table ?