I’m having a hard time understanding the difference the two lines bellow. The only difference is the usage of :url =>
1- The following line in my view is generating an error (removing form_for removes the error and rendering is done with no error):
<%= form_for( [@company, @appointment], :action => 'company_edit', :html => {:class => "form-horizontal"}) do |f| %>
I spent a lot of time trying to make sense of the error, but I do not know where ‘company_edit’is coming from.
ActionView::Template::Error (undefined method `company_appointment_path' for #<#<Class:0x5047cc8>:0x5045070>):
9: </div>
10: <div class="widget-content nopadding">
11: <!--form action="#" method="get" class="form-horizontal"-->
12: <%= form_for([@company, @appointment], :action => 'company_edit', :html => {:class => "form-horizontal"}) do |f| %>
13: <% if @appointment.errors.any? %>
14: <div class="control-group">
15: <div id="error_explanation">
app/views/appointments/company_edit.html.erb:12:in `_app_views_appointments_company_edit_html_erb___287242072_42128364'
Here is a snippet of my route file:
resources :companies do
resources :appointments, only: [:company_edit] do
member do
get 'company_edit', :as => :company_edit
end
end
end
2- I found that using :url, made all work again, but why?
<%= form_for([@company, @appointment], :url => {:action => 'company_edit'}, :html => {:class => "form-horizontal"}) do |f| %>
I’m trying to understand this since I use ‘form_for’ in many places and I have never used ‘:url =>’ before and I want to know if I was not using ‘form_for’ correctly and that I now need to update my code to properly use the helper.
EDIT
This is my controller:
def company_edit
@appointment = Appointment.find(params[:id])
@company = Company.find(params[:company_id])
end
I’m afraid the answer to your question is no.
:actionis not an options parameter recognized byform_for.Here’s the API:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
:urlis an option it recognizes and itself includes an options hash which includes:action.I hope that helps.