Reputation: 5073
I'm getting an error "First argument in form cannot contain nil or be empty" for
<%= form_for @assessment, :method=>"get", :url => url_for(:action => 'new') do |f| %>
I get the error when re-rendering the :options page after an invalid submission. What's wrong with the way I've set this up?
Step 1: User needs to choose a template and a patient_id in Assessment#Options
def options
@assessment = current_user.assessments.new
@patients = current_user.patients.sort_by{|e| e.first_name.downcase}
@patient = current_user.patients.new
@templates = Template.all
end
Assessment#options view:
<%= form_for @assessment, :method=>"get", :url => url_for(:action => 'new') do |f| %>
<% if @assessment.errors.any? %>
<div>
<%= pluralize(@assessment.errors.count, "error") %> prevented the account from being created:
<ul>
<% @assessment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
...
<%= f.submit "Create Assessment", :class => "btn btn-primary", :style=>"width:100%;", :id=>"options-submit" %>
<% end %>
On submit, I GET this action:
def new
if params[:assessment][:template_id].present? && params[:assessment][:patient_id].present?
@assessment = current_user.assessments.new
@assessment.template = Template.find(params[:template_id])
@assessment.patient = current_user.patients.find(params[:patient_id])
else
render :options
end
end
Upvotes: 0
Views: 79
Reputation: 18080
You need @assessment
to be instantiated. You can move it outside the condition.
def new
@assessment = current_user.assessments.new
if params[:assessment][:template_id].present? && params[:assessment][:patient_id].present?
@assessment.template = Template.find(params[:template_id])
@assessment.patient = current_user.patients.find(params[:patient_id])
else
render :options
end
end
Upvotes: 1
Reputation: 618
The line
render :options
does not execute the action options in your controller. It only renders the view using the variables in the new action. As a result none of your instance variables are actually being being set, so your form craps out.
Try moving the stuff in the options method into your if-then loop.
Upvotes: 0