I am trying to create a conditional edit button, i.e. create if record does not exist, edit otherwise. I have somewhat accomplished this like so:
<% if !appl.hvac_environment.nil? %>
<%= link_to 'Review', edit_hvac_environment_path(HvacEnvironment.where("appliance_id = ?", :appl), :a => appl) %>
<% else%>
<%= link_to 'Review', new_hvac_environment_path(:a => appl)%></td>
<% end %>
appl is the appliance in my form, which has a ‘hvac_environment’ association. I am passing :a to retrieve the appl id to store in the hvac_env object’s field.
My contoller then attempts to find the appropriate record to edit, but cannot find by id in ‘edit’ method because the id passed is an active record object: Couldn’t find HvacEnvironment with id=#ActiveRecord::Relation:0x9fbe8f8>. Why does my form pass this and how can I pass the hvac_env id so I can edit the appropriate object.
This works when there is no record, so im sure the problem is with the second line, any thoughts?
From your code I assume that you have Appliance model that has has_one relationship with HvacEnvironment
In this case this should work