1

私はrails3を使用していて、簡単な調査アプリケーションを作成しようとしていますが、いくつかの問題が発生しています。質問は問題なく作成できますが、人々が回答できる調査を作成する場合、いくつかの問題に遭遇します。1つは、回答に:user_idまたは:survey_idが割り当てられていないことです。以下は私のコントローラー、モデル、ビューのコピーです。

サーベイコントローラー

def take
  @survey = Survey.find(params[:id])
  @questions = @survey.questions
  @answers = @questions.map{|q| q.answers.build}
  @answers.each do |a|
    a.survey_id = @survey.id
    a.user_id = current_user.id
  end

  respond_to do |format|
    format.html #
  end
end

def submit
  @survey = Survey.find(params[:id])

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

調査モデル

class Survey < ActiveRecord::Base
  belongs_to :user
  has_many :questions, :dependent => :destroy
  has_many :answers, :through => :questions, :dependent => :destroy
  validates_presence_of :name, :user_id
  accepts_nested_attributes_for :questions, :allow_destroy => true
end

質問モデル

    class Question < ActiveRecord::Base
     belongs_to :survey
     has_many :answers
     validates_presence_of :question_string, :question_type
     accepts_nested_attributes_for :answers, :allow_destroy => true
    end

回答モデル

    class Answer < ActiveRecord::Base
     belongs_to :question
     belongs_to :user
     belongs_to :survey
    end

ビューを見る

    %h1 Take the survey
    - semantic_form_for @survey, :url => { :action => "submit" } do |s|
     - if @survey.errors.any?
        %p= pluralize(@survey.errors.count, "error") + " prohibited this survey from being saved"
        #survey_errors
         - @survey.errors.full_messages.each do |err_msg|
            %p= err_msg
     #survey_details.header
     = s.fields_for :questions do |q|
        = q.fields_for :answers do |a|
         = render 'answer_fields', :s => a

     .actions
        = s.commit_button

部分的な質問

    #questions_form.fields
     = s.input :question_string, :label => "Question: "
     = s.input :question_type, :as => :radio, :collection => [1,2,3,4], :label => "Question Type:"
     = s.hidden_field :_destroy
     = link_to_function  "Remove Question", "remove_fields(this)"

部分的な回答

    #answers_form.fields
     = s.label :answer_string, "Answer"
     = s.text_field :answer_string
     = s.hidden_field :question_id

Routes.rb

    resources :surveys do
        get 'take', :on => :member
        put 'submit', :on => :member
    end
4

1 に答える 1

0

回答の部分にユーザー ID を含む別の非表示フィールドを追加してみてください。

s.hidden_field :user_id, :value => x

それが私が隠しフィールドを行った方法です。そこに変数を入れているだけです。キーと値のペアとして渡さないことでマージの問題が発生しました。


何を使っていますか?HAML、それがどのように機能しているのかよくわかりません。

これは、すべてが機能するために必要なすべてです。

調査モデル

has_many :answers
accepts_nested_attributes_for :answers, :allow_destroy => true

コントローラーに追加のコードは必要ありません

ビューが必要fields_for

 <% f.fields for :answers do |f| %>
     <%= f.hidden_field :user_id, :value => current_user.id %>
 <% end %>
于 2010-10-28T00:53:02.000 に答える