Rails 3 嵌套资源编辑/更新方法 - 路由错误

发布于 2024-11-19 07:49:38 字数 1096 浏览 3 评论 0原文

我需要一些有关嵌套资源操作的帮助。我有三个嵌套资源:作业、问题和答案。我目前只想让编辑/更新方法适用于问题控制器。关系如下:Jobs has_many questions 且 Questions 属于 Jobs。

我正在对问题使用编辑操作,但收到错误:

No route matches "/jobs/1/questions"

我不明白为什么。

我目前将此代码作为我的问题控制器中的编辑和更新操作:

def edit
 @job = Job.find(params[:job_id])
 @question = @job.questions.find(params[:id])
end

def update
  @job = Job.find(params[:job_id])
  @question = @job.questions.find(params[:id])

  if @question.update_attributes(params[:question])
    redirect_to(@question)
  end
end

模型:

class Job < ActiveRecord::Base
has_many :questions

class Question < ActiveRecord::Base
belongs_to :job

路线:

  resources :jobs do
   resources :questions do
    resources :answers
   end
 end

我不明白的事情是: a)为什么它将我重定向到问题索引路径,而我没有将其重定向到那里,以及 b) 它说这不是有效的路由,但如果我刷新该确切的 URL,页面就会正确加载。

我尝试了多种选择,但无法找出解决方案。

感谢您的帮助。如果您需要更多信息,请告诉我。

ps这是我的rake路线: https://gist.github.com/1077134

I need some help with nested resource actions. I have three nested resources: Jobs, Questions and Answers. I am currently only trying to get the edit/update method to work for the questions controller. The relationship is as so: Jobs has_many questions and Questions belong to Jobs.

I am using the edit action on the questions and am getting an error:

No route matches "/jobs/1/questions"

and I cannot figure out why.

I currently have this code as my edit and update action in my Questions controller:

def edit
 @job = Job.find(params[:job_id])
 @question = @job.questions.find(params[:id])
end

def update
  @job = Job.find(params[:job_id])
  @question = @job.questions.find(params[:id])

  if @question.update_attributes(params[:question])
    redirect_to(@question)
  end
end

Models:

class Job < ActiveRecord::Base
has_many :questions

class Question < ActiveRecord::Base
belongs_to :job

Routes:

  resources :jobs do
   resources :questions do
    resources :answers
   end
 end

The things that I don't understand are:
a) why is it redirecting me to the questions index path, when I didn't redirect it there, and
b) It says that is not a valid route, but if I refresh that exact URL the page loads properly.

I have tried multiple options, but I can't figure out the solution.

Thanks for the help. Let me know if you need more info.

p.s. here is my rake routes : https://gist.github.com/1077134

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2024-11-26 07:49:38

为了让您开始,
在 view/jobs/show.rb 中:

<%= link_to 'Edit', edit_jobs_path(@job) %>

在 vi​​ew/questions/show.rb 中:

<%= link_to 'Edit', edit_job_question_path(@question.job, @question) %>

在 vi​​ew/questions/edit.rb 中:

<%= link_to 'Show', job_question_path %>

我要显示的是链接需要有嵌套模式。如果您的答案有很多评论,您最终可能会得到以下结果:
edit_job_question_answer_comment(@job,@question,@answer,@comment)
其中 @symboled 变量是在控制器中导出的。
希望这有帮助!

您稍后可能想要:

class Job < ActiveRecord::Base
  has_many :questions
  has_many :answer, :through => :questions

  # If you want to edit the questions of a job whilst editing a job then research accepts nested attributes
  #accepts_nested_attributes_for :questions, :allow_destroy => true
end

To get you started,
In view/jobs/show.rb :

<%= link_to 'Edit', edit_jobs_path(@job) %>

In view/questions/show.rb :

<%= link_to 'Edit', edit_job_question_path(@question.job, @question) %>

In view/questions/edit.rb :

<%= link_to 'Show', job_question_path %>

What I'm showing is that the links need to have a nested pattern. If your answers had many comments, you might end up with things like:
edit_job_question_answer_comment(@job, @question, @answer, @comment)
where the @symboled variables are derived in the controller.
Hope this helps!

You may later want:

class Job < ActiveRecord::Base
  has_many :questions
  has_many :answer, :through => :questions

  # If you want to edit the questions of a job whilst editing a job then research accepts nested attributes
  #accepts_nested_attributes_for :questions, :allow_destroy => true
end
墨落画卷 2024-11-26 07:49:38

事实证明,我的问题比我最初想象的要复杂一些。我的数据库和表设置不正确,他们无法为我的资源找到正确的 :ids。我必须首先像这样规范化我的表格:

class CreateQuestions < ActiveRecord::Migration
def self.up
create_table :questions do |t|
   t.references :job
  t.text :question1
  t.text :question2
  t.text :question3
  t.text :question4
  t.text :question5
  t.text :question6
  t.text :question7
  t.text :question8
  t.text :question9
  t.text :question10

  t.timestamps
end
end

这种设置是重复的、肮脏的,并且弄乱了问题控制器的操作。所以我将其更改为:

def self.up
create_table :questions do |t|
  t.references :job
  t.text :question

  t.timestamps
end
end

并在我的作业(父资源)new_form 视图中创建了带有循环的nested_forms。

<%= form_for(@job) do |f| %>
 <%= f.label :name %><br />
 <%= f.text_field :name %>
<%= f.fields_for :questions do |builder| %>
 <%= f.label :question, "Question" %><br \>
 <%= f.text_area :question, :rows => 10 %>
<% end %>

执行此操作后,我的所有控制器方法都更加干净,并且编辑/更新操作正常工作。

这就是我解决问题的方法,这可能不是最好的方法。另外,如果您有任何需要添加的内容或对我的代码有任何疑问,请告诉我,我会看看是否可以提供帮助。

谢谢!

So it turns out that my issue was a little more involved than I had originally thought. My database and tables were not setup properly and they were having trouble finding the proper :ids for my resources. I had to start by normalizing my tables like so:

class CreateQuestions < ActiveRecord::Migration
def self.up
create_table :questions do |t|
   t.references :job
  t.text :question1
  t.text :question2
  t.text :question3
  t.text :question4
  t.text :question5
  t.text :question6
  t.text :question7
  t.text :question8
  t.text :question9
  t.text :question10

  t.timestamps
end
end

This set up was repetitive and dirty and it was messing up questions controller actions. So I changed it to:

def self.up
create_table :questions do |t|
  t.references :job
  t.text :question

  t.timestamps
end
end

and created nested_forms with loops in my jobs (the parent resource) new_form view.

<%= form_for(@job) do |f| %>
 <%= f.label :name %><br />
 <%= f.text_field :name %>
<%= f.fields_for :questions do |builder| %>
 <%= f.label :question, "Question" %><br \>
 <%= f.text_area :question, :rows => 10 %>
<% end %>

After doing this all of my controller methods were cleaner and the edit/update action was working properly.

This is how I solved the issue, it may not be the best way to do so. Also, if you have anything to add or any questions about my code, just let me know and I'll see if I can help.

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文