Rails,简单表单,嵌套表单(Rails, Simple Form, Nested Forms)

编程入门 行业动态 更新时间:2024-10-28 06:36:41
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制作应用程序。

我有一些名为projects,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

ProjectAnswer:

belongs_to :project_question#, counter_cache: true belongs_to :user

我的目标是在我的项目展示页面上显示部分内容,该页面显示与项目相关的问题及其答案,并链接到另一个表单,您可以在其中提问或回答问题。 我在挣扎。

在我的控制器中,我有:

项目:

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

然后在我看来,我有:

项目#显示:

<% 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'用于#<#:0x000001013d8298>`

我无法看到它在表格块外面谈论f的位置。 我尝试在项目的simple_fields的表单中添加另一行,然后将项目问题表单嵌套在其中,但这不会改变任何内容。

谁能看到我做错了什么?

谢谢

class Project has_many :project_questions, dependent: :destroy#, through: :projects accepts_nested_attributes_for :project_questions end

I am trying to make an app with rails 4 and Simple Form.

I have models called projects, project_questions and project_answers.

The associations between them are:

Projects:

has_many :project_questions, dependent: :destroy#, through: :projects accepts_nested_attributes_for :project_questions

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

Project answers:

belongs_to :project_question#, counter_cache: true belongs_to :user

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

ProjectAnswer:

belongs_to :project_question#, counter_cache: true belongs_to :user

My objective is to have a partial on my projects show page which displays the questions and their answers relating to the project and to have a link to another form where you can ask a question or answer one. I am struggling.

In my controllers I have:

Projects:

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

Then in my views I have:

Project#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 (a partial):

<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>

My project_questions form partial is:

<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 (a partial):

(not yet written - still trying to get the questions to work)

My routes are:

resources :projects do resources :project_questions do resources :project_answers end end

When I try this, I get an error in my project_questions form that says:undefined local variable or methodf' for #<#:0x000001013d8298>`

I can't see where it is talking about f outside the form block. I have tried adding another line to the form for simple_fields for project and then nesting the project questions form inside of it, but that doesn't change anything.

Can anyone see what I've done wrong?

Thank you

最满意答案

您应该更改此行<% f.simple_fields_for :project_questions do |f| %> <% f.simple_fields_for :project_questions do |f| %>到此<%= simple_form_for :project_questions do |f| %> <%= simple_form_for :project_questions do |f| %>以使其工作。

You should change this line <% f.simple_fields_for :project_questions do |f| %> to this <%= simple_form_for :project_questions do |f| %> in order to make it work.

更多推荐

本文发布于:2023-07-28 08:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1303067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   嵌套   简单   Rails   Forms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!