ruby - Rails NoMethodError Undefined method in nested form -


i new rails , having trouble creating new model within nested form.

undefined method `days' nil:nilclass

<%= form_for([@schedule, @schedule.days.build]) |d| %>     <%= d.label :date %><br/>     <%= d.text_field :date %><br/>     <%= d.submit %><br/> <% end %> 

my days controller looks :

class dayscontroller < applicationcontroller   def create       @schedule = schedule.find(params[:schedule_id])       @day = @schedule.days.create(params[:day].permit(:date))   end end 

and models follows:

class day < activerecord::base   belongs_to :schedule end  class schedule < activerecord::base   belongs_to :event   belongs_to :user   has_many :days, dependent: :destroy   accepts_nested_attributes_for :days, allow_destroy: true end  class event < activerecord::base   belongs_to :user   has_many :links, dependent: :destroy   has_one :schedule, dependent: :destroy   has_many :days, through: :schedule    accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: true   accepts_nested_attributes_for :schedule, allow_destroy: true   accepts_nested_attributes_for :days, allow_destroy: true    validates :name, :description, presence: true end 

also routes:

rails.application.routes.draw    devise_for :users   resources :events       resources :schedules   end    resources :schedules     resources :days   end    root "page#home" end 

you're defining @schedule in create action, form rendered after calling new action, means @schedule undefined @ point.

i'd try stick rails convention of new/create, looks like

class dayscontroller   def new     @day = schedule.find(params[:schedule_id]).days.new   end    def create     @day = day.new(params[:day])     if @day.save       redirect_to days_path # or wherever want go     else       render 'new'     end   end end 

to clearer, order of events is:

  • call days#new action visiting /schedule/:schedule_id/days/new route
  • the form rendered
  • user inputs values , hits 'submit'
  • this calls days#create action
  • the new day created based on values user put form.

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -