嵌套资源的形式

发布于 2024-11-16 04:26:52 字数 1145 浏览 3 评论 0原文

我已经浏览了大量的 form_for 嵌套资源问题,但无法找到任何适合我的解决方案。我想是时候问一个个性化的问题了。

我有两个模型,工作和问题,工作有很多问题和问题属于工作。

我使用脚手架创建控制器和模型,然后将资源嵌套在 paths.rb 中。

root :to => "pages#home"

resources :jobs do
   resources :questions
end

get "pages/home"
get "pages/about"
get "pages/contact"


class Job < ActiveRecord::Base
   has_many :questions
end

class Question < ActiveRecord::Base
   belongs_to :job
end

现在我正在尝试访问“/jobs/1/questions/new”并不断收到

Questions#new 中的 NoMethodError

我从错误 No Route matches {:controller=>"questions"} 当代码是

<%= form_for(@question) do |f| %>

我知道这是错误的,所以我开始尝试其他组合,但没有一个起作用。

我已经尝试

 <%= form_for([@job.questions.build ]) do |f| %>

 <%= form_for([@job, @job.questions.build ]) do |f| %>

<%= form_for(@job, @question) do |f| %>

一堆其他组合中,但不起作用。

这是我的 rake 路线的链接: git clone https://gist.github.com/1032734

感谢任何帮助并让我知道您是否需要更多信息,谢谢。

I have gone through tons of the form_for nested resource questions and can't get any of the solutions to work for me. I figured its time to ask a personalized question.

I have two models, jobs and questions, jobs has_many questions and questions belong_to jobs.

I used scaffolding to create the controllers and models then nested the resources in the routes.rb.

root :to => "pages#home"

resources :jobs do
   resources :questions
end

get "pages/home"
get "pages/about"
get "pages/contact"


class Job < ActiveRecord::Base
   has_many :questions
end

class Question < ActiveRecord::Base
   belongs_to :job
end

Right now I am trying to access '/jobs/1/questions/new' and keep getting the

NoMethodError in Questions#new

I started with the error No route matches {:controller=>"questions"} when the code was

<%= form_for(@question) do |f| %>

I know this is wrong, so I started to try other combos and none of them worked.

I've tried

 <%= form_for([@job.questions.build ]) do |f| %>

that

 <%= form_for([@job, @job.questions.build ]) do |f| %>

that

<%= form_for(@job, @question) do |f| %>

Among a bunch of other combinations and that are not working.

Here is a link to my rake routes : git clone https://gist.github.com/1032734

Any help is appreciated and let me know if you need more info, thanks.

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

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

发布评论

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

评论(2

北风几吹夏 2024-11-23 04:26:52

我只是将 URL 作为额外选项传递:

<%= form_for(@question, :url => job_questions_path(@job)) do %>

编辑:

另请尝试:

form_for([@job, @question])

I just pass the URL as an extra option:

<%= form_for(@question, :url => job_questions_path(@job)) do %>

EDIT:

Also try:

form_for([@job, @question])
野却迷人 2024-11-23 04:26:52

这就是我解决我的问题的方法:)

在您的 questions/_form.html.erb 中,

<%= form_for [@job, @question] do %>

为了使其正常工作,您需要作业的 id。您将按如下方式传递它:
questions_controller.rb 中,

def new
  @job = Job.find(params[job_id])
  @question = @job.questions.build
end

Build(.build) 与上面代码中使用 new(.new) 类似,仅在旧版本中有所不同导轨版本;导轨 2 向下。

现在进行创建操作(仍在 questions_controller.rb 中)

def create
  @job = Job.find(params[:job_id])
  @question = @job.questions.build(question_params)
end

如果您仅使用这些操作,则问题模型中的 job_id 和 user_id 字段将为空。要添加 id,请执行以下操作:
在您的 questions_controller.rb 中,将 job_id 添加到 job_params 中,如下所示:

def question_params
  params.require(:question).permit(:ahaa, :ohoo, :job_id)
end

然后要传递用户的 id(如果您使用的是 Devise),请执行以下操作:

def create
  @job = Job.find(params[:job_id])
  @question = @job.questions.build(question_params)
  @question.user_id = current_user.id
end

This is how I solved mine :)

In your questions/_form.html.erb

<%= form_for [@job, @question] do %>

For this to work, you need the job's id. You'll pass it as follows:
In the questions_controller.rb

def new
  @job = Job.find(params[job_id])
  @question = @job.questions.build
end

Build(.build) is similar to using new(.new) in the code above, with differences only in older versions of rails; rails 2 down.

Now for the create action (still in questions_controller.rb)

def create
  @job = Job.find(params[:job_id])
  @question = @job.questions.build(question_params)
end

If you only use these, the job_id and user_id field in the question model will be empty. To add the ids, do this:
In your questions_controller.rb add job_id to job_params like so:

def question_params
  params.require(:question).permit(:ahaa, :ohoo, :job_id)
end

Then to pass the user's id (if you are using Devise), do:

def create
  @job = Job.find(params[:job_id])
  @question = @job.questions.build(question_params)
  @question.user_id = current_user.id
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文