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

时间:2018-04-02 13:46:24

标签: ruby-on-rails nested-forms

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

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

这是我的代码:

QuestionsController:

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

Answer.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:answers,它会为问题的每个答案呈现一个字段,我相信这是因为我在“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”=&gt;中那里有额外的“0”。难道我做错了什么?任何建议或帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

那些参数行看起来很奇怪,但实际上它是has_many关系的嵌套表单的工作方式,所以大多数情况下你实现的一切都正确,但我认为你不需要嵌套表单。

如果你想创建或更新一个或多个属于它的答案,你需要嵌套表格,只需一个请求。

在这里你只想创建一个答案,你不想触摸问题。所以你可以简化整个事情 - 只为答案创建表格。

questions_controller.rb:

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

由于您使用js中的AnswersController回复,我认为您的表单应该是远程的。

的问题/ 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 views文件夹。

答案/ _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

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