нужен пример nested_form - PullRequest
       4

нужен пример nested_form

1 голос
/ 05 октября 2011

Я сдаюсь. Я пытаюсь создать простую вложенную форму с двумя моделями после эпизода Railscasts # 196 и не работает. Может кто-нибудь отправить рабочий пример, пожалуйста, чтобы я мог проверить в моей среде. Я использую 3.1.0

Например, когда я пытаюсь построить 3 вопроса в форме, появляется только 1 поле вопроса, тогда аргумент survey_id не передается.

Буду признателен за вашу помощь после 2 дней и ночей. Я пропустил что-то действительно большое. Спасибо

Модель

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

class Question < ActiveRecord::Base
  belongs_to :survey
  attr_accessible :survey_id, :name
end

Контроллер

def new
@survey = Survey.new
4.times { @survey.questions.build }

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

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

View

<%= 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 |msg| %>
      <li><%= msg %></li>
     <% end %>
   </ul>
  </div>
<% end %>

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

<%= fields_for :questions do |builder| %>
  <%= builder.label :name, "Question"  %>
  <%= builder.text_field :name %>
<% end %>

<br /><br />
<div class="actions">
 <%= f.submit %>
</div>
<% end %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...