问题
I have Q+A model inside an events model and having issue with understanding how the nested routes work. I'm getting a No route matches {:action=>"create", :controller=>"event_questions" and
missing required keys: [:event_id]`
My question form sits inside my show.hrml.erb for my event model right now. Some of the relationship stuff is from neo4j gem so it isn't standard but the issue shouldn't be related to that. From what I know, I am posting to /events/events_id/event_questions
events_controller.rb
def show
@event = Event.find(params[:id])
@event_question = EventQuestion.new
end
event.rb
has_many :out, :event_questions, type: 'questions_of'
event_question.rb
has_one :in, :events, origin: :event_questions
events/show.html.erb
<%= form_for [:event, @event_question] do |f| %>
#form stuff
<% end %>
event_questions_controller.rb
def create
@event_question = EventQuestion.new(event_question_params)
if @event_question.save
@event = Event.find(params[:event_id])
@event_question.update(admin: current_user.facebook_id)
@event_question.events << @event
redirect_to @event
else
redirect_to :back
end
end
routes.rb
resources :events do
resources :event_questions, only: [:create, :destroy]
end
回答1:
I think it's in your routes. Change it to look like this:
resources :events do
resources :questions, controller: 'event_questions`, only: [:create, :destroy]
end
That'll give you http://youraddress/event/:event_id/questions
and put it in the expected controller. Make sure it's event_questions.rb
and not events_questions.rb
. From terminal, also run rake routes
and it'll show you the actions, paths, and controllers responsible for them.
回答2:
Got it to work with
form_for(@event_question, :url => event_event_questions_path(@event)) do |f|
I'm not sure how to pass the @event via the other way (in the question), but assuming you can, both methods should work.
来源:https://stackoverflow.com/questions/26900360/rails-nested-routing-issue