I am working on a nested resources project, and am getting an error in my steps controller:
def create
@step = Step.new(step_params)
respond_to do |f|
if @step.save
f.html { redirect_to @step, notice: 'Step was successfully created.' }
f.json { render action: 'show', status: :created, location: @step }
else
f.html { render action: 'new' }
f.json { render json: @step.errors, status: :unprocessable_entity }
end
end
end
The error I am getting is:
undefined method `step_url' for #<StepsController:0x007feeb6442198>
My routes looks like this:
root 'lists#index'
resources :lists do
resources :steps
end
As you are using nested resources, update the create action as below:
def create
@list = List.find(params[:list_id])
@step = @list.steps.build(step_params) ## Assuming that list has_many steps
respond_to do |f|
if @step.save
## update the url passed to redirect_to as below
f.html { redirect_to list_step_url(@list,@step), notice: 'Step was successfully created.' }
f.json { render action: 'show', status: :created, location: @step }
else
f.html { render action: 'new' }
f.json { render json: @step.errors, status: :unprocessable_entity }
end
end
end
Run rake routes
to see the available routes.
As route to steps#show would look something like
GET lists/:list_id/steps/:id steps#show
Use list_step_url
with 2 arguments @list
For :list_id and @step
for :id to go to show page of Step.