[英]No route matches [POST] “/organizations/new”
在我的Rails應用程序中,我獲得了很多資源,並且已經創建了一些表單-但是由於某種原因,我似乎沒有一種特定的表單來使新對象正常工作。 我不確定是因為我正在使用三向has_many:through關系,還是因為我只是在忽略其他東西
這是我的路線的樣子
resources :users, shallow: true do
resources :organizations, :notifications
end
resources :organizations, shallow: true do
resources :plans, :users, :notifications
end
我的organizations_controller看起來像這樣:
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
在我的組織索引頁面上,我鏈接到此:
<%= button_to 'New Organization', new_organization_path, :class => 'btn btn-primary' %>
這應該導致我的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 %>
每次單擊“新組織”,都會出現以下錯誤:
No route matches [POST] "/organizations/new"
正確-我沒有接受POST請求的new_organizations_path。 我知道我可以將表格的方法手動更改為GET,但是它不應該像我那樣工作嗎? 我還有另一種形式,它遵循相同的原理,只是用於不同的資源,並且效果很好。
謝謝您的幫助!
除非指定了其他內容,否則button_to
將始終發送POST
請求。 在另一種形式上,您必須使用link_to
而不是button_to
,這就是它在那里工作的原因。
您可以通過兩種方式更改button_to
,選擇一種適合您的方式:
<%= link_to 'New Organization', new_organization_path, :class => 'btn btn-primary' %>
<%= button_to 'New Organization', new_organization_path, method: :get, :class => 'btn btn-primary' %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.