ruby - NoMethodError in Rails when creating a new instance of model -
i have model depends model, have specified in routes.rb
file
rails.application.routes.draw resources :events resources :guests :whereabouts end devise_for :users, :controllers => { registrations: 'registrations' }
models/event.rb
:
class event < activerecord::base belongs_to :user has_many :guests end
models/guest.rb
:
class guest < activerecord::base belongs_to :event end
when access http://localhost:3000/events/2/guests/
works when access http://localhost:3000/events/2/guests/new
get
undefined method `guests_path' #<#<class:0x00000004074f78>:0x0000000aaf67c0>
from line #1 of views/guests/_form.html.erb
file is
<%= form_for(@guest) |f| %> <% if @guest.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@guest.errors.count, "error") %> prohibited event being saved:</h2> <ul> <% @guest.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.text_field :first_name %> </div> <div class="field"> <%= f.text_field :last_name %> </div> <div class="field"> <%= f.text_field :email %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
i changed paths proper ones since guests_path
should event_guests_path
, don't know change in form work properly.
any clue? routing wrong?
your routing correct. guest object depends event 1 have change form to:
<%= form_for [@event, @guest] |f| %> ... <% end %>
you must have initialized @event variable in controller did like:
@event = event.find(params[:event_id]) @guest = @event.guests.build
Comments
Post a Comment