Get parent attribute within new child form?
- by dannymcc
I have a simple Rails 3 application and I am trying to create a new record that belongs to it's owner. It's working by passing the id to a hidden field in the new form of the child record.
This works well and once the new child form submitted it correctly gets associated in the child/parent relationship.
What I am trying to do, is lookup values form the parent within the new child form. The problem is that the child relationship is not yet created. Is there anyway I can use a .where lookup in the view? Or, is there a better way of doing this?
At the moment I am passing the animal_id though to the new Claim form and it's inserted into a hidden field labelled animal_id.
What I am trying to do:
<%= @animal.where(:animal_id => params[:animal_id]).id %>
The above would ideally get the animal ID from the soon-to-be-associated animal. Is there any sort of before_filter or anything that could take the passed params from the URL and temporarily create the relationship just for the new form view and then permanently create the relationship once the form is submitted?
I've tried adding the following to my Claims controller and then called @animal.AnimalName in the view but I get NoMethodError:
before_filter :find_animal
protected
def find_animal
if params[:animal_id]
Animal.find(params[:animal_id])
end
end
The URL of the new claim is correctly showing the animal ID so I'm not sure why it's not finding it:
http://localhost:3000/claims/new?animal_id=1
The model relations are as follows:
animal has_many claims
animal has_one exclusion
claim has_one animal
exception has_one animal