2016-09-25 6 views
0

My Rails 5アプリケーションには、アクティビティとタイムスロットの2つのモデルが含まれています。このネストされたフォームを使用して新しいオブジェクトを作成する方法

activity.rb

class Activity < ApplicationRecord 
    belongs_to :club 
    has_many :timeslots, :dependent => :destroy 
    accepts_nested_attributes_for :timeslots 
    validates :club_id, presence: true 
    validates :name, presence: true, length: { maximum: 50 } 
end 

timeslot.rb

class Timeslot < ApplicationRecord 
    belongs_to :activity 
    validates :time_start, presence: true 
    validates :time_end, presence: true 
    validates :day, presence: true 
    #validates :activity_id, presence: true (I realised this was causing one of the errors I had) 
    default_scope -> { order(day: :asc) } 
end 

私は私の活動を作成するときに、私もそれは同じページ上の最初のタイムスロット、同じフォームの作成したいと思います。この

new.html.erb

<%= form_for(@activity) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field"> 
     <%= f.label :name, "Class name" %>* 
     <%= f.text_field :name, class: 'form-control' %> 

      <%= f.fields_for :timeslots do |timeslots_form| %> 
        <%= timeslots_form.label :time_start, "Start time" %> 
        <%= timeslots_form.time_select :time_start %> 

        <%= timeslots_form.label :time_end, "End time" %> 
        <%= timeslots_form.time_select :time_end %> 

        <%= timeslots_form.label :day %> 
        <%= timeslots_form.select :day, (0..6).map {|d| [Date::DAYNAMES[d], d]} %> 
      <% end %> 
    </div> 
    <%= f.submit "Create class", class: "btn btn-primary" %> 
<% end %> 

私の編集/更新バージョンが正常に動作しているようです。

class ActivitiesController < ApplicationController 
... 

def new 
    @activity = Activity.new 
    @activity.timeslots.build 
end 

def create 
    @activity = current_club.activities.build(activity_params) 
    #@activity.timeslots.first.activity_id = @activity.id (I thought this might solve the problem, but didn't) 
    if @activity.save 
     flash[:success] = "New class created!" 
     redirect_to activities_path 
    else 
     render 'new' 
    end 
end 

def edit 
    @activity = current_club.activities.find_by(id: params[:id]) 
    @activity.timeslots.build 
end 

def update 
    @activity = current_club.activities.find_by(id: params[:id]) 
    if @activity.update_attributes(activity_params) 
     flash[:sucess] = "Class updated!" 
     redirect_to edit_activity_path(@activity) 
    else 
     render 'edit' 
    end 
end 
... 

private 

    def activity_params 
     params.require(:activity).permit(:name, :active, #active is set to default: true 
             :timeslots_attributes => [:id, 
                    :time_start, 
                    :time_end, 
                    :day, 
                    :active]) 
    end 
end 

activities_controller.rb

しかし、私は、「タイムスロット活動が存在しなければなりません」というエラーメッセージが表示されます新しいアクティビティを作成しようとするたびに。

アクティビティが作成される前にtimelotにactivity_idを割り当てようとしているように感じますが、わかりません。私は多くのことを試してきましたが(いくつかは私のコメントの例で私の例に含まれています)、なぜこのエラーが出ているのか分かりません。

更新:追加のエラーログ

Started POST "/activities" for 127.0.0.1 at 2016-09-25 18:04:51 +0700 
Processing by ActivitiesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"xp+dBcWC4cjI6FLpIqhU0RzM4ldZ4JpkFLSyAXcmifL73QqWz6R65EHm/Tj7QxlXnWiBA0axjVXvMZHQ+XKA9A==", "activity"=>{"name"=>"Newest class", "timeslots_attributes"=>{"0"=>{"time_start(1i)"=>"2016", "time_start(2i)"=>"9", "time_start(3i)"=>"25", "time_start(4i)"=>"12", "time_start(5i)"=>"00", "time_end(1i)"=>"2016", "time_end(2i)"=>"9", "time_end(3i)"=>"25", "time_end(4i)"=>"13", "time_end(5i)"=>"30", "day"=>"4"}}}, "commit"=>"Create class"} 
    Club Load (0.2ms) SELECT "clubs".* FROM "clubs" WHERE "clubs"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 
    (0.1ms) begin transaction 
    (0.1ms) rollback transaction 
    Rendering activities/new.html.erb within layouts/application 
    Rendered shared/_error_messages.html.erb (1.5ms) 
    Rendered activities/new.html.erb within layouts/application (16.9ms) 
    Rendered layouts/_rails_default.html.erb (58.5ms) 
    Rendered layouts/_shim.html.erb (0.5ms) 
    Rendered layouts/_header.html.erb (1.7ms) 
    Rendered layouts/_footer.html.erb (0.8ms) 
Completed 200 OK in 111ms (Views: 93.9ms | ActiveRecord: 0.3ms) 
+0

の詳細を学ぶことができ、あなたからのあなたの完全なエラーログを表示します端末 – luissimo

+0

エラーログを含めるように私の質問が更新されました – Rhys0h

+0

ログにエラーが表示されず、コードが正常に表示されるため、目撃しているエラーはどこですか? – luissimo

答えて

0

レール5は、デフォルトで必要な属性BELONGS_TOなりますので、あなたはこのエラーを取得しています。また、省メカニズムは一種の、次のとおりです。これを解決する

validate your parent model 
validate your child model # validation fails here because parent doesn't have an id yet, because it hasn't been saved 
save parent model 
save child model 

方法があるに:

class Activity < AR 
    has_many :timeslots, dependent: :destroy, inverse_of: :activity 
    accepts_nested_attributes_for :timeslots 
end 

class Timeslot < AR 
    belongs_to :activity, inverse_of: :timeslot 
end 

あなたはこのherehere

+0

ありがとうございます。フォームが現在機能しています。あなたが提供した2番目のリンクは、私が直面していた問題を解決しました。 – Rhys0h

関連する問題