Ruby 接受rails3.1rc4的\u嵌套\u属性\u
我在模型中使用accepts_nested_attributes_,但我的子窗体未保存在数据库中 我正在构建一个嵌套表单,几乎与Ryan Bates railscasts在第196/197集中的方式相同。我有一张家长问题表和作为孩子的答案表: models/question.rbRuby 接受rails3.1rc4的\u嵌套\u属性\u,ruby,ruby-on-rails-3,nested-forms,Ruby,Ruby On Rails 3,Nested Forms,我在模型中使用accepts_nested_attributes_,但我的子窗体未保存在数据库中 我正在构建一个嵌套表单,几乎与Ryan Bates railscasts在第196/197集中的方式相同。我有一张家长问题表和作为孩子的答案表: models/question.rb class Question < ActiveRecord::Base belongs_to :user has_many :answers, :dependent => :destroy ac
class Question < ActiveRecord::Base
belongs_to :user
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? },
:allow_destroy => true
validates :content, :presence => true
end
类问题:毁灭
接受_嵌套的_属性_for:answers,:reject _if=>lambda{a | a[:text].blank?},
:allow_destroy=>true
验证:content,:presence=>true
结束
模型/答案.rb
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
class-Answer
控制器/问题\u controller.rb
class QuestionsController < ApplicationController
before_filter :authenticate_user!
def index
setup_questions
end
def create
@question = Question.new(params[:question])
#this is to get every id_key from the user into the params of the answer
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
@question.user_id = current_user.id
if @question.save
redirect_to questions_path, :notice => "Successfully created question."
else
setup_questions
render :index
end
end
def edit
@question = Question.find(params[:id])
end
def update
@question = Question.find(params[:id])
respond_to do |format|
if @question.update_attributes(params[:question])
format.html { redirect_to(@question, :notice => 'Question was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @question.errors, :status => :unprocessable_entity }
end
end
end
def show
@question = Question.find(params[:id])
end
def destroy
@question = Question.find(params[:id])
#authorize! :destroy, @question
@question.destroy
redirect_to questions_path, :notice => "Successfully deleted question: #{@question.content}."
end
private
def setup_questions
@questions = Question.all
@question ||= Question.new
@question.answers.build #to build the answers form
end
end
类问题控制器“已成功创建问题。”
其他的
设置问题
渲染:索引
结束
结束
定义编辑
@question=question.find(参数[:id])
结束
def更新
@question=question.find(参数[:id])
回应待办事项|格式|
如果@question.update_属性(参数[:question])
format.html{redirect_to(@question,:notice=>“问题已成功更新”)}
format.xml{head:ok}
其他的
format.html{render:action=>“edit”}
format.xml{render:xml=>@question.errors,:status=>:unprocessable_entity}
结束
结束
结束
def秀
@question=question.find(参数[:id])
结束
def销毁
@question=question.find(参数[:id])
#授权!:摧毁,@问题
@问题.销毁
将_重定向到问题路径:notice=>“已成功删除问题:#{@question.content}。”
结束
私有的
def设置问题
@问题
@问题| |=新问题
@question.answers.build#构建答案表单
结束
结束
视图/问题/_form.html.erb
<%= form_for(@question) do |f| %>
<!-- =================== -->
<!-- = Error handeling = -->
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<!-- =================== -->
<!-- ================= -->
<!-- = Question form = -->
<div class="field">
<%= f.label :content, "Question" %><br />
<%= f.text_field :content, :placeholder => "type your question here.." %>
</div>
<!-- ====================== -->
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<% end %>
</div>
<!-- ====================== -->
<div class="actions">
<%= f.submit %>
</div>
<!-- ================= -->
<% end %>
禁止保存此问题:
“在此处键入您的问题…”%>
“键入可选答案…”%>
所以现在最大的问题是。。。为什么它不将应答字段中的任何内容保存到数据库中?我认为在父级(问题)执行一次创建操作就足够了,并且“接受\u嵌套的\u属性\u for”应该处理它的子级
问候,,
Thijs1从控制器中删除此线路
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
2将此添加到您的视图中
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<% builder.object.user = current_user %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<%= builder.hidden_field, :user_id %>
<% end %>
</div>
<!-- ====================== -->
“键入可选答案…”%>
1从控制器中删除此线路
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
2将此添加到您的视图中
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<% builder.object.user = current_user %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<%= builder.hidden_field, :user_id %>
<% end %>
</div>
<!-- ====================== -->
“键入可选答案…”%>
谢谢,但答案表中仍然没有保存任何内容。。。?我真的不明白为什么?我想我已经知道我做错了什么。。。愚蠢,愚蠢…:接受嵌套的属性用于:答案,:拒绝如果=>lambda{a}a[:text].blank?},:允许销毁=>true必须是:接受嵌套的属性用于:答案,:拒绝如果=>lambda{a}a[:content].blank?},:允许销毁=>truethanks,但答案表中仍然没有保存任何内容。。。?我真的不明白为什么?我想我已经知道我做错了什么。。。愚蠢,愚蠢…:接受嵌套的属性用于:答案,:拒绝如果=>lambda{a}a[:text].blank?},:允许销毁=>true必须是:接受嵌套的属性用于:答案,:拒绝如果=>lambda{a}a[:content]。blank?},:允许销毁=>true