Reputation: 325
In my rails app I got quite a few resources and have created a few forms already - but for some reason I don't seem to get one specific form for a new object to work. I am not sure if it is because I am using a three-way has_many :through relationship or because I am just overlooking something else
Here's how my routes looks like
resources :users, shallow: true do
resources :organizations, :notifications
end
resources :organizations, shallow: true do
resources :plans, :users, :notifications
end
My organizations_controller looks like this:
def index
@user = current_user
@organizations = @user.organizations.to_a
end
def show
@user = current_user
@organization = Organization.find(params[:id])
end
def new
@organization = Organization.new
end
def create
@user = current_user
@organization = Organization.new(organization_params)
@organization.save
redirect_to @organization
end
On my organizations index page I link to this:
<%= button_to 'New Organization', new_organization_path, :class => 'btn btn-primary' %>
which should lead to my new.html.erb:
<%= form_for (@organization) do |f| %>
<%= render 'layouts/messages' %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :website %>
<%= f.text_area :website, class: 'form-control' %>
</div>
<%= f.button :class => 'btn btn-primary' %>
<% end %>
Every time I click on "new Organization" I get following error:
No route matches [POST] "/organizations/new"
Which is correct - I do not have a new_organizations_path that accepts POST requests. I know I can manually change the method of the form to GET but shouldn't it work the way I did it? I have another form that follows the same principle just for a different resource and it works perfectly.
Thanks for your help in advance!
Upvotes: 0
Views: 114
Reputation: 53018
button_to
would always send a POST
request unless something else is specified.
On the other form, you must be using link_to
and not button_to
which is why its working there.
You can change the button_to
in two ways, pick the one that suits you:
<%= link_to 'New Organization', new_organization_path, :class => 'btn btn-primary' %>
<%= button_to 'New Organization', new_organization_path, method: :get, :class => 'btn btn-primary' %>
Upvotes: 1