2016-06-18 215 views
1

就我所知,一切看起來都很棒 - 但field_for嵌套窗體的內容並未顯示我想要的3個問題窗體。爲什麼?nested fields_for not not up in forms

survey.rb

class Survey < ActiveRecord::Base 
    has_many :questions, :dependent => :destroy 
    accepts_nested_attributes_for :questions 
end 

question.rb

class Question < ActiveRecord::Base 
    belongs_to :survey 
end 

surveys_controller.rb

class SurveysController < ApplicationController 
    before_action :set_survey, only: [:show, :edit, :update, :destroy] 

    # GET /surveys 
    # GET /surveys.json 
    def index 
    @surveys = Survey.all 
    end 

    # GET /surveys/1 
    # GET /surveys/1.json 
    def show 
    end 

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

    # GET /surveys/1/edit 
    def edit 
    end 

    # POST /surveys 
    # POST /surveys.json 
    def create 
    @survey = Survey.new(survey_params) 

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

    # PATCH/PUT /surveys/1 
    # PATCH/PUT /surveys/1.json 
    def update 
    respond_to do |format| 
     if @survey.update(survey_params) 
     format.html { redirect_to @survey, notice: 'Survey was successfully updated.' } 
     format.json { render :show, status: :ok, location: @survey } 
     else 
     format.html { render :edit } 
     format.json { render json: @survey.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /surveys/1 
    # DELETE /surveys/1.json 
    def destroy 
    @survey.destroy 
    respond_to do |format| 
     format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_survey 
     @survey = Survey.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def survey_params 
     params.require(:survey).permit(:name) 
    end 
end 

調查/ new.html.erb

<h1>New Survey</h1> 

<%= render 'form' %> 

<%= link_to 'Back', surveys_path %> 

調查/ _form.html.erb

<%= form_for(@survey) do |f| %> 
    <% if @survey.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2> 

     <ul> 
     <% @survey.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 


    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 

    <% f.fields_for :questions do |builder| %> 
    <p> 
    <%= builder.label :content, "Question" %><br /> 
    <%= builder.text_area :content, :rows => 3 %> 
    </p> 
    <% end %> 



    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

回答

1

不能相信我這樣做 - 在fields_for

失蹤 <%=