I have two models, a Charity model and a Milestone model. A charity has_many milestones.
To keep the interface a bit simpler, I’m putting the small milestone form on the show view of the Charity controller. This is fine, but when the charity model fails to save, how do i go back and render those objects with the show action so I get their invalid state?
Here’s my create action on the milestones controller. Presently I’m getting a model_name error for nil, meaning that the instance variables are likely not set.
def create
@charity = Charity.find(params[:charity_id])
@charity.milestones.build(params[:milestone])
if @charity.save
redirect_to @charity, notice: "Milestone added"
else
render 'charities/show'
end
end
I know you can normally do just render action :new, but since this is across controllers, it does not work. Ideas?
Forgot to set the
@milestoneinstance variable. Silly mistake. Hope this helps someone down the road.