表格总结额外的空白项目

时间:2017-12-20 16:01:26

标签: ruby-on-rails string

当我提交答案表格时,第一项是获取唯一的空字符串,我如何解决它并指导我。

额外项目图片:

enter image description here

问题模型

class Question < ApplicationRecord
  has_many :answers, dependent: :destroy
end

回答模型

class Answer < ApplicationRecord
  belongs_to :question
end

回答控制器

 class AnswersController < ApplicationController
  def create
    @question = Question.find(params[:question_id])
    @answer = @question.answers.create(params[:answer].permit(:body))
    redirect_to question_path(@question)
  end
  def destroy
    @question = Question.find(params[:question_id])
    @answer = @question.answers.find(params[:id])
    @answer.destroy
    redirect_to question_path(@question)
  end
end

回复表格

    <%= form_for([@question,@question.answers.build]) do |f| %>
    <div class="row">
        <div class="input-field col s12 m9">
          <%= f.text_area :body ,:id=>"ans_form" %>
          <label for="first_name"style="text-indent:15px;line-height:15px">Create Possible Answer</label>
        </div>
        <div class="input-field col s12 m3">
          <%= button_tag(type: 'submit', class: "col s12 btn waves-effect accent-4") do %>
            Create Answer
          <% end %>
        </div>
    </div>
<% end %>

在我看来(显示页面)

<%= @question.answers.each do |answer| %>
  <%= answer.body %>
  <%= link_to 'delete',[answer.question, answer], method: :delete  %>
<% end %>

0 个答案:

没有答案