表格在提交后变为空白

时间:2013-10-22 00:43:03

标签: ruby-on-rails ruby-on-rails-4

我不知道发生了什么,但我的问题表格停止了工作,现在提交后变成了空白。

以下是表单视图:

<%= simple_form_for [@comment, Question.new] do |f| %>
<p>
<div class="form">
<%= f.input :title, as: :text, input_html: { rows: "1" } %>
<%= f.input :body, as: :text, input_html: { rows: "10" } %>

<p><%= f.submit "Answer", class: "btn btn-primary" %></p>
</div>
<% end %>

问题模型:

class Question < ActiveRecord::Base
validates :body, presence: true
validates :title, presence: true
validates :user_id, presence: true

belongs_to :user
acts_as_votable
belongs_to :comment
has_many :pictures

end

和控制器:

 def create
 @comment = Comment.find(params[:comment_id])
 @question = @comment.questions.create(question_params)

 respond_to do |format|
  if @question.save
  format.html { redirect_to comment_questions_path, notice: 'Question was successfully created.' }  
    format.json { render action: 'show', status: :created, location: comment_questions_path }
  else
    format.html { render action: 'new' }
    format.json { render json: @question.errors, status: :unprocessable_entity }
  end
 end
end

1 个答案:

答案 0 :(得分:0)

主要问题在于路由:你不能将嵌套资源重定向到@question。使用

url_for([@comment, @question])