Rails, 简单表格, 嵌套表格
Rails, Simple Form, Nested Forms
class Project
has_many :project_questions, dependent: :destroy#, through: :projects
accepts_nested_attributes_for :project_questions
end
我正在尝试使用 rails 4 和 Simple Form 制作一个应用程序。
我有名为项目的模型,project_questions 和 project_answers。
它们之间的关联是:
项目:
has_many :project_questions, dependent: :destroy#, through: :projects
accepts_nested_attributes_for :project_questions
项目问题:
belongs_to :project#, counter_cache: true
has_many :project_answers, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :project_answers
belongs_to :user
项目答案:
belongs_to :project_question#, counter_cache: true
belongs_to :user
用户:
has_many :project_questions
has_many :project_answers
class ProjectQuestions
belongs_to :project#, counter_cache: true
has_many :project_answers, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :project_answers
end
项目答案:
belongs_to :project_question#, counter_cache: true
belongs_to :user
我的 objective 是在我的项目显示页面上有一个部分,它显示与项目相关的问题和他们的答案,并有一个 link 到另一个你可以提问的表格或回答一个。我在挣扎。
在我的控制器中我有:
项目:
def project_params
params.require(:project).permit(project_question_attributes: [:title, :content, :user_id, :project_id,project_answer_attributes: [:answer, :project_question_id]],
Project_question:
def new
@project_question = ProjectQuestion.new
@project_id = params[:project_id]
@project_question.project_answers[0] = ProjectAnswer.new
end
Project_answer:
def new
@project_answer = ProjectAnswer.new
end
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id])
end
那么在我看来我有:
项目#show:
<% if current_user.id == @project.creator_id %>
<%= link_to 'Ask a question', new_project_project_question_path(:project_id => @project.id) %>
<% end %>
<%= render 'project_questions/pqps' %>
Project_question#pqps(部分):
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="categorytitle">
<%= @project_question.try(:title) %>
</div>
<div class="generaltext">
<%= @project_question.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == @project.creator_id %>
<% end %>
</span>
</div>
</div>
</div>
我的 project_questions 表格部分是:
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% f.simple_fields_for :project_questions do |f| %>
<%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %>
<%= f.input :title, label: 'Question:', :label_html => {:class => 'categorytitle'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', class: 'categorytitle'} %>
<%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'categorytitle'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', rows: 5, class: 'generalprojecttext'} %>
<%= f.button :submit %>
<% end %>
</div>
</div>
</div>
Project_answer#pa(部分):
(not yet written - still trying to get the questions to work)
我的路线是:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
当我尝试此操作时,我在 project_questions 表单中收到一条错误消息:undefined local variable or method
f' for #<#:0x000001013d8298>`
我看不到它在表单块外的什么地方谈论 f。我尝试在项目的 simple_fields 表单中添加另一行,然后在其中嵌套项目问题表单,但这并没有改变任何东西。
谁能看出我做错了什么?
谢谢
plataformatec simple_form wiki 有一个很好的 instructional page on using nested forms。
(作为旁注,我发现不使用局部变量通常更容易诊断问题。一旦一切正常,您可以通过重构为局部变量来清理。)
我相信你需要将一个局部变量传递给你的局部变量,所以它知道第一个 "f" 指的是什么。
<%= render 'project_questions/pqps', locals: {f: f} %>
(您也可以选择在 project_questions 的局部变量中使用不同的局部变量,这样您就可以始终清楚错误消息中引用的内容。)
您应该将这一行 <% f.simple_fields_for :project_questions do |f|
%>
更改为这一行 <%= simple_form_for :project_questions do |f| %>
以使其正常工作。
class Project
has_many :project_questions, dependent: :destroy#, through: :projects
accepts_nested_attributes_for :project_questions
end
我正在尝试使用 rails 4 和 Simple Form 制作一个应用程序。
我有名为项目的模型,project_questions 和 project_answers。
它们之间的关联是:
项目:
has_many :project_questions, dependent: :destroy#, through: :projects
accepts_nested_attributes_for :project_questions
项目问题:
belongs_to :project#, counter_cache: true
has_many :project_answers, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :project_answers
belongs_to :user
项目答案:
belongs_to :project_question#, counter_cache: true
belongs_to :user
用户:
has_many :project_questions
has_many :project_answers
class ProjectQuestions
belongs_to :project#, counter_cache: true
has_many :project_answers, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :project_answers
end
项目答案:
belongs_to :project_question#, counter_cache: true
belongs_to :user
我的 objective 是在我的项目显示页面上有一个部分,它显示与项目相关的问题和他们的答案,并有一个 link 到另一个你可以提问的表格或回答一个。我在挣扎。
在我的控制器中我有:
项目:
def project_params
params.require(:project).permit(project_question_attributes: [:title, :content, :user_id, :project_id,project_answer_attributes: [:answer, :project_question_id]],
Project_question:
def new
@project_question = ProjectQuestion.new
@project_id = params[:project_id]
@project_question.project_answers[0] = ProjectAnswer.new
end
Project_answer:
def new
@project_answer = ProjectAnswer.new
end
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id])
end
那么在我看来我有:
项目#show:
<% if current_user.id == @project.creator_id %>
<%= link_to 'Ask a question', new_project_project_question_path(:project_id => @project.id) %>
<% end %>
<%= render 'project_questions/pqps' %>
Project_question#pqps(部分):
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="categorytitle">
<%= @project_question.try(:title) %>
</div>
<div class="generaltext">
<%= @project_question.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == @project.creator_id %>
<% end %>
</span>
</div>
</div>
</div>
我的 project_questions 表格部分是:
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% f.simple_fields_for :project_questions do |f| %>
<%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %>
<%= f.input :title, label: 'Question:', :label_html => {:class => 'categorytitle'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', class: 'categorytitle'} %>
<%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'categorytitle'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', rows: 5, class: 'generalprojecttext'} %>
<%= f.button :submit %>
<% end %>
</div>
</div>
</div>
Project_answer#pa(部分):
(not yet written - still trying to get the questions to work)
我的路线是:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
当我尝试此操作时,我在 project_questions 表单中收到一条错误消息:undefined local variable or method
f' for #<#:0x000001013d8298>`
我看不到它在表单块外的什么地方谈论 f。我尝试在项目的 simple_fields 表单中添加另一行,然后在其中嵌套项目问题表单,但这并没有改变任何东西。
谁能看出我做错了什么?
谢谢
plataformatec simple_form wiki 有一个很好的 instructional page on using nested forms。
(作为旁注,我发现不使用局部变量通常更容易诊断问题。一旦一切正常,您可以通过重构为局部变量来清理。)
我相信你需要将一个局部变量传递给你的局部变量,所以它知道第一个 "f" 指的是什么。
<%= render 'project_questions/pqps', locals: {f: f} %>
(您也可以选择在 project_questions 的局部变量中使用不同的局部变量,这样您就可以始终清楚错误消息中引用的内容。)
您应该将这一行 <% f.simple_fields_for :project_questions do |f|
%>
更改为这一行 <%= simple_form_for :project_questions do |f| %>
以使其正常工作。