Metaphysiker Metaphysiker - 6 months ago 32
Ruby Question

"undefined method `model_name' for..." error when using nested resources and simple_form, ruby on rails

enter code here
I get an "undefined method `model_name' for {:url=>"/possibilities/65/date_events/new"}:Hash" error, when I use simple_form in nested resources.

This is my date_events/new.html.erb:

<%= simple_form_for([@possibility, @dateevent, url: new_possibility_date_event_path]) do |f| %>


I once tried this:

<%= simple_form_for([@possibility, @dateevent]) do |f| %>


I got this error-message:

undefined method `date_events_path' for #<#<Class:0x007ffb4165e138>:0x007ffb42056f78>


My routes:

event_possibilities GET /events/:event_id/possibilities(.:format) possibilities#index
POST /events/:event_id/possibilities(.:format) possibilities#create
new_event_possibility GET /events/:event_id/possibilities/new(.:format) possibilities#new
edit_event_possibility GET /events/:event_id/possibilities/:id/edit(.:format) possibilities#edit
event_possibility GET /events/:event_id/possibilities/:id(.:format) possibilities#show
PATCH /events/:event_id/possibilities/:id(.:format) possibilities#update
PUT /events/:event_id/possibilities/:id(.:format) possibilities#update
DELETE /events/:event_id/possibilities/:id(.:format) possibilities#destroy
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
possibility_date_events GET /possibilities/:possibility_id/date_events(.:format) date_events#index
POST /possibilities/:possibility_id/date_events(.:format) date_events#create
new_possibility_date_event GET /possibilities/:possibility_id/date_events/new(.:format) date_events#new
edit_possibility_date_event GET /possibilities/:possibility_id/date_events/:id/edit(.:format) date_events#edit
possibility_date_event GET /possibilities/:possibility_id/date_events/:id(.:format) date_events#show
PATCH /possibilities/:possibility_id/date_events/:id(.:format) date_events#update
PUT /possibilities/:possibility_id/date_events/:id(.:format) date_events#update
DELETE /possibilities/:possibility_id/date_events/:id(.:format) date_events#destroy
possibilities GET /possibilities(.:format) possibilities#index
POST /possibilities(.:format) possibilities#create
new_possibility GET /possibilities/new(.:format) possibilities#new
edit_possibility GET /possibilities/:id/edit(.:format) possibilities#edit
possibility GET /possibilities/:id(.:format) possibilities#show
PATCH /possibilities/:id(.:format) possibilities#update
PUT /possibilities/:id(.:format) possibilities#update
DELETE /possibilities/:id(.:format) possibilities#destroy
root GET /


Any advices? Thanks in advance!

A possible problem might be, that I have two different nested resources, like this:

resources :events do
resources :possibilities
end

resources :possibilities do
resources :date_events
end


edit: Here is my create action for possibility. It redirects to:

redirect_to new_possibility_date_event_path(@possibility)

def create
@possibility = Possibility.new
@possibility.event_id = params[:event_id]

if @possibility.save

else
render 'new'
end
redirect_to new_possibility_date_event_path(@possibility)
end


edit 2: Here is my date_events controller:

def new
@dateevent = DateEvent.new
@datelist = "Test"
@date = params[:date] ? Date.parse(params[:date]) : Date.today
@first_calendar_day = @date.beginning_of_month.beginning_of_week(:monday)
@last_calendar_day = @date.end_of_month.end_of_week(:monday)
@weeks = (@first_calendar_day..@last_calendar_day).to_a.in_groups_of(7)
end

def create
@possibility = Possibility.find(params[:possibility])
@datelist = params[:date].split(",")
@datelist.each do |date|
params[:date_event][:date] = date
@dateevent = DateEvent.new(dateevents_params)
@dateevent.save
end
redirect_to kalender_path
end
end

private

def dateevents_params
params.require(:date_event).permit(:date)
end

Answer

try this:

<%= simple_form_for([@possibility, @dateevent], url: new_possibility_date_event_path(@possibility)) do |f| %>

or

<%= simple_form_for([@possibility, @dateevent], :url=> "/possibilities/#{@possibility.id}/date_events/new") do |f| %>

Add below line to new action of date_event

@possibility = Possibility.find_by_id(params[:possibility_id])