2012-08-03 73 views
1

我知道一些r​​ailscasts可能很老,但修改不应该是这样。我努力克服嵌套形式,但我得到的大部分答案是使用插件。所以我尝试从头开始制作电视广播,但发生异常。我想知道什么是今天的Railcasts 196更新版本,并带有正确的代码。嵌套表格(Railscast#196)

在这里,我的代码,也许是我的一个愚蠢的错误。

测量模型

class Survey < ActiveRecord::Base 
    has_many :questions, :dependent => :destroy 
    accepts_nested_attributes_for :questions , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 
    attr_accessible :name, :questions_attributes 
end 

问题型号

class Question < ActiveRecord::Base 
    belongs_to :survey 
    attr_accessible :content, :question_id 

    accepts_nested_attributes_for :answers , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 
    attr_accessible :name, :answers_attributes 
end 

应答模型

class Answer < ActiveRecord::Base 
    belongs_to :questions 
    attr_accessible :content, :question_id 
end 

调查显示窗体

<p id="notice"><%= notice %></p> 

<div>Name:</div> 
<div><%[email protected]%></div> 
<% for question in @survey.questions %> 
<div><%=h question.content%></div> 
<% end %> 

<%= link_to 'Edit', edit_survey_path(@survey) %> | 
<%= link_to 'Back', surveys_path %> 

Form.html.erb创建一个新的苏当

<%= form_for(@survey) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 

    <%= f.fields_for :questions do |bf|%> 
    <% render 'question_fields, :f => bf %> 
    <% end %> 
    ***<div class="actions">*** 
    <%= f.submit %> 
    </div> 
<% end %> 

question_field形式

<%= f.label :content, "Question" %><br /> 
<%= f.text_area :content, :rows=> 3 %><br /> 
<%= f.check_box :_destroy %> 
<%= f.label :_destroy, "Remove Question"%><br /> 

<%= f.fields_for :answer do |form| %> 
    <%= render 'answer_fields', :f => form %> 
<% end %> 

answer_field形式

<%= f.label :content, "Answer" %> 
<%= f.text_field :content %> 
<%= f.check_box :_destroy %> 
<%= f.label :_destroy, "Remove" %> 

测量控制器

class SurveysController < ApplicationController 
    # GET /surveys 
    # GET /surveys.json 
    def index 
    @surveys = Survey.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @surveys } 
    end 
    end 

    # GET /surveys/1 
    # GET /surveys/1.json 
    def show 
    @survey = Survey.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @survey } 
    end 
    end 

    # GET /surveys/new 
    # GET /surveys/new.json 
    def new 
    @survey = Survey.new 
    3.times {@survey.questions.build } 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @survey } 
    end 
    end 

    # GET /surveys/1/edit 
    def edit 
    @survey = Survey.find(params[:id]) 
    end 

    # POST /surveys 
    # POST /surveys.json 
    def create 
    @survey = Survey.new(params[:survey]) 

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

    # PUT /surveys/1 
    # PUT /surveys/1.json 
    def update 
    @survey = Survey.find(params[:id]) 

    respond_to do |format| 
     if @survey.update_attributes(params[:survey]) 
     format.html { redirect_to @survey, notice: 'Survey was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @survey.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /surveys/1 
    # DELETE /surveys/1.json 
    def destroy 
    @survey = Survey.find(params[:id]) 
    @survey.destroy 

    respond_to do |format| 
     format.html { redirect_to surveys_url } 
     format.json { head :no_content } 
    end 
    end 
end 

这里我的错误rvey 不知道还有什么可能是错误的

ArgumentError in SurveysController#create  
No association found for name `answers'. Has it been defined yet? 

在这里,新的错误

SyntaxError in Surveys#new 

Showing /home/jean/rail/surveysays/app/views/surveys/_form.html.erb where line #22 raised: 

/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:22: syntax error, unexpected keyword_class, expecting keyword_do or '{' or '(' 
@output_buffer.safe_concat(' <div class="actions"> 
             ^
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: unterminated regexp meets end of file 
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: syntax error, unexpected $end, expecting keyword_end 

回答

2

添加has_many :answers到问题的模型,并更改应答模式belongs_to :question

+0

它解决了关联错误,但我有一个缺少结束标记错误 – Jseb 2012-08-03 20:49:16

+0

在哪里?什么是错误? – jordanpg 2012-08-03 20:51:36

+0

我刚刚添加了表单,它在div class =“”部分 – Jseb 2012-08-03 20:52:14