在"show.html.erb"中使用简单表单(嵌套表单)不会将数据保存到数据库



我一直在尝试在我的 questions/show.html.erb中使用嵌套表单或简单表单,但是当我单击"提交"时,它会给我一些奇怪的参数。

我有很多答案的问题。我希望能够在"问题显示"视图中创建答案。

这是我的代码:

问题controller:

def new
 @question = Question.new
 @question.answers.build
end
def create
 @question = Question.new(question_params)
 if @question.save
  redirect_to question_path
 else
  render 'new'
 end
end
def show
  @question = Question.find(params[:id])
  @question.answers.build
end
def edit
  @question = Question.find(params[:id])
  @question.answers.build
end
def update
  @question = Question.find(params[:id])
  if @question.update(question_params)
    redirect_to question_path
  else
    render :edit
  end
end
def question_params
 params.require(:question).permit(:id, :question_title, :question_text, answers_attributes: [ :id, :answer_text, :question_id, :_destroy])
end

AnswersController.rb

def new
  @answer = Answer.new
end
def create
  @answer = Answer.new(@answer_params)
  @answer.question = Question.find(params[:question_id])
  @answer.save
  respond_to do |format|
    format.js
  end
end
def edit
  @answer = Answer.find(params[:id])
end
def update
  @answer = Answer.find(params[:id])
  if @answer.update(answers_params)
    redirect_to answers_path
  else
    render :edit
  end
end

Question.RB

class Question < ApplicationRecord
  belongs_to :topic
  belongs_to :user
  has_many :answers, dependent: :destroy
  accepts_nested_attributes_for :answers, allow_destroy: true
end

答案。RB

class Answer < ApplicationRecord
  belongs_to :question
end

问题/show.html.erb

<div class=question-show> 
  <div class="question-title"> <%= @question.question_title %></div>
  <div class="question_text"> <%= @question.question_text %></div>
  <div class="question-user"><%= question_show_timestamp(@question) %> </div>
  <div class="question-topic"> <strong>Category:</strong> <%= @question.topic.category.category_name %></div>
  <div class="question-topic"> <strong>Topic:</strong> <%= @question.topic.topic_name %></div>
  <div class="question-answers">
    <%= render "form" %>
    <hr>
    <% @question.answers.select(&:persisted?).each do |answer| %>
      <div> 
        <div> <%= answer.answer_text %> </div>
        <div class="small-font"> <%= answer_timestamp(answer) %> by <%= answer.user.user_name %> </div>
      </div>
      <hr>
    <% end %>
  </div>
</div>

_form.html.erb

<%= simple_form_for @question do |form| %>
  <div class="TabbedPanelsContent">
    <%= form.simple_fields_for(:answers, @question.answers.build) do |builder| %>
      <%= render "answer_fields", form: builder %>
    <% end %>
    <p><%= link_to_add_fields "Add answer", form, :answers %></p>
  </div>
  <%= form.submit "Save" %>
<% end %>

请注意,我正在使用

simple_fields_for(:answers, @question.answers.build)

如果我以simple_fields_for:答案将其写成,它将为每个答案提供一个字段,我相信这是因为我在" show.html.erb"视图中使用了此simple_form,但是我是不确定。对此的任何评论都将非常有帮助!

_answer_fields.html.erb

<div class="fields">
  <p>
    <%= form.input_field :_destroy, as: :hidden %>
    <%= link_to "Remove", "#", class: "remove_record" %>
  </p>
  <p class="lable">Answer</p>
  <p class="field"><%= form.text_field :answer_text %></p></br>
</div>

这是我单击"保存:

"时发生的情况
Started PATCH "/questions/19" for 172.18.0.1 at 2018-04-02 13:15:06 +0000
Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by QuestionsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"yhLrwHi8wgPRx30ysPakqcKfb+Pjd3BhWN4F/7K5m+FaraCpi7iq6RcrT98q/ISGTv/g8ZlcrDX1ItnrRTBgfQ==", "question"=>{"answers_attributes"=>{"0"=>{"_destroy"=>"false", "answer_text"=>"wer"}}}, "commit"=>"Save", "id"=>"19"}
  Question Load (0.8ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT $2  [["id", 19], ["LIMIT", 1]]
   (0.4ms)  BEGIN
  Topic Load (0.8ms)  SELECT  "topics".* FROM "topics" WHERE "topics"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
   (0.5ms)  ROLLBACK
  Rendering questions/edit.html.erb within layouts/application
  Rendered questions/edit.html.erb within layouts/application (0.6ms)
  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Category Load (0.8ms)  SELECT "categories".* FROM "categories"
  Rendered layouts/_navbar.html.erb (7.2ms)
  Rendered questions/_new.html.erb (3.2ms)
Completed 200 OK in 416ms (Views: 392.0ms | ActiveRecord: 4.8ms)

对我来说看起来很有趣的线是:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"yhLrwHi8wgPRx30ysPakqcKfb+Pjd3BhWN4F/7K5m+FaraCpi7iq6RcrT98q/ISGTv/g8ZlcrDX1ItnrRTBgfQ==", "question"=>{"answers_attributes"=>{"0"=>{"_destroy"=>"false", "answer_text"=>"wer"}}}, "commit"=>"Save", "id"=>"19"}

您可以看到,在" answers_attributes" =>中有n个额外的" 0"。难道我做错了什么?任何建议或帮助将不胜感激。

该参数行似乎在您看来很奇怪,但实际上这是has_many关系嵌套形式的方式,因此您大多是您实现了一切,但是我认为您不需要嵌套的形式在这里。

,如果您想创建或更新问题,并且一个或一个或多个答案,则需要一个请求。

在这里,您只想创建一个答案,您不想遇到问题。因此,您可以简化整个过程 - 仅为答案创建表单。

Question_controller.rb:

def show
  @question = Question.find(params[:id])
  @answer = @question.answers.build
end

由于您在AnswersController中使用js响应,我认为您的表格应该是远程的。

问题/show.html.erb:

<div class="question-answers">
  <%= simple_form_for @answer, remote: true do |f| %>
    <%= f.hidden_field @answer.question_id $>
    <%= render 'answers/form_fields', f: f %>
    <%= f.submit "Save" %>
</div>

(不要忘记将question_id添加到AnswersController中的允许参数(

一件好事是将表单字段移动以回答answers视图文件夹。

答案/_form_fields.html.erb:

<div class="fields">
  <p class="lable">Answer</p>
  <p class="field">
    <%= f.text_field :answer_text %>
  </p>
  </br>
</div>

之后,您应该在提交时看到日志中的下一个(如果您的路线一切正常(:

Processing by AnswersController#create

然后我会更改您的AnswersController

def create
  @answer = Answer.create(@answer_params)
end

无需明确设置问题,因为现在您通过参数设置了问题。如果您仅响应一种格式,则无需指定格式。

最新更新