In Rails 3, I have two models, Nutrient and Recommendation. Nutrient has_many Recommendation, Recommendation belongs_to Nutrient. I am trying to implement the create method of the recommendation in the controller. Is this the proper way to do it:
def create
nutrient_id = params[:recommendation][:nutrient_id]
if nutrient_id.blank?
#nutrient_id was blank in the submit, get other recommendation params and re-render 'new'
params[:recommendation].delete(:nutrient_id)
@recommendation=Recommendation.new(params[:recommendation])
render 'new'
else
@nutrient = Nutrient.find(nutrient_id)
if @nutrient
#nutrient was found by id, create recommendation
@recommendation = @nutrient.recommendations.build(params[:recommendation])
if @recommendation.save
redirect_to @recommendation
else
render 'new'
end
else
#nutrient was not found by id, get other recommendation params and re-render 'new'
params[:recommendation].delete(:nutrient_id)
@recommendation = Recommendation.new(params[:recommendation])
render 'new'
end
end
end