我正在处理Rails中的嵌套资源“farm”,我制作新农场的表单如下所示:
<%= form_for([@user, @farm], :url=> new_user_farm_path(@user)) do |f| %>
<% if @farm.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@farm.errors.count, "error") %> prohibited this farm from being saved:</h2>
<ul>
<% @farm.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :contact %><br />
<%= f.text_field :contact %>
</div>
<div class="field">
<%= f.label :adress %><br />
<%= f.text_field :adress %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
农场控制器中相应的“新”功能是:
def new
@farm = Farm.new
@user = User.find(current_user)
respond_to do |format|
format.html # new.html.erb
format.json { render json: @farm }
end
end
它渲染表单很好,但在我单击提交后,它实际上尝试创建新的Farm,我收到此错误:
No route matches [POST] "/users/2/farm/new"
在我的佣金路线中,我显然有路线显示:
user_farm POST /users/:user_id/farm(.:format) {:action=
create", :controller=>"farms"}
我只是猜测问题出在我的创建函数中:
def create
@farm = Farm.new(params[:farm])
@user = User.find(current_user)
respond_to do |format|
if @farm.save
format.html { redirect_to user_farm(@user, @farm), notice: 'Farm was successfully created.' }
format.json { render json: @farm, status: :created, location: @farm }
else
format.html { render action: "new" }
format.json { render json: @farm.errors, status: :unprocessable_entity }
end
end
end
我的routes.rb文件:
resources :users do
resource :farm
end
devise_for :users, :path => 'accounts'
我通过以下链接访问我的新农场表单:
<%= link_to "New Farm", new_user_farm_path(current_user) %>
我的整个佣金路线:
user_farm POST /users/:user_id/farm(.:format) {:action=>"
create", :controller=>"farms"}
new_user_farm GET /users/:user_id/farm/new(.:format) {:action=>"
new", :controller=>"farms"}
edit_user_farm GET /users/:user_id/farm/edit(.:format) {:action=>"
edit", :controller=>"farms"}
GET /users/:user_id/farm(.:format) {:action=>"
show", :controller=>"farms"}
PUT /users/:user_id/farm(.:format) {:action=>"
update", :controller=>"farms"}
DELETE /users/:user_id/farm(.:format) {:action=>"
destroy", :controller=>"farms"}
users GET /users(.:format) {:action=>"
index", :controller=>"users"}
POST /users(.:format) {:action=>"
create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"
new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"
edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"
show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"
update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"
destroy", :controller=>"users"}
new_user_session GET /accounts/sign_in(.:format) {:action=>"
new", :controller=>"devise/sessions"}
user_session POST /accounts/sign_in(.:format) {:action=>"
create", :controller=>"devise/sessions"}
destroy_user_session GET /accounts/sign_out(.:format) {:action=>"
destroy", :controller=>"devise/sessions"}
user_password POST /accounts/password(.:format) {:action=>"
create", :controller=>"devise/passwords"}
new_user_password GET /accounts/password/new(.:format) {:action=>"
new", :controller=>"devise/passwords"}
edit_user_password GET /accounts/password/edit(.:format) {:action=>"
edit", :controller=>"devise/passwords"}
PUT /accounts/password(.:format) {:action=>"
update", :controller=>"devise/passwords"}
cancel_user_registration GET /accounts/cancel(.:format) {:action=>"
cancel", :controller=>"devise/registrations"}
user_registration POST /accounts(.:format) {:action=>"
create", :controller=>"devise/registrations"}
new_user_registration GET /accounts/sign_up(.:format) {:action=>"
new", :controller=>"devise/registrations"}
edit_user_registration GET /accounts/edit(.:format) {:action=>"
edit", :controller=>"devise/registrations"}
PUT /accounts(.:format) {:action=>"
update", :controller=>"devise/registrations"}
DELETE /accounts(.:format) {:action=>"
destroy", :controller=>"devise/registrations"}
user_confirmation POST /accounts/confirmation(.:format) {:action=>"
create", :controller=>"devise/confirmations"}
new_user_confirmation GET /accounts/confirmation/new(.:format) {:action=>"
new", :controller=>"devise/confirmations"}
GET /accounts/confirmation(.:format) {:action=>"
show", :controller=>"devise/confirmations"}
user_unlock POST /accounts/unlock(.:format) {:action=>"
create", :controller=>"devise/unlocks"}
new_user_unlock GET /accounts/unlock/new(.:format) {:action=>"
new", :controller=>"devise/unlocks"}
GET /accounts/unlock(.:format) {:action=>"
show", :controller=>"devise/unlocks"}
home_index GET /home/index(.:format) {:controlle
r=>"home", :action=>"index"}
root / {:controlle
r=>"home", :action=>"index"}
答案 0 :(得分:3)
问题是您的表单正在尝试向仅存在于GET请求的URL发出POST请求。所以它告诉你
[POST] "/users/2/farm/new"
不存在 - 它没有。而你的rake routes
输出确认了这一点 - 最奇怪的是
new_user_farm GET /users/:user_id/farm/new(.:format)
这是一个GET请求。
表单默认使用POST来创建新记录,因此您需要提供可以POST到的URL。或者你可以让Rails从对象的状态中找出它。所以要么
<%= form_for([@user, @farm], :url=> user_farm_path(@user)) do |f| %>
OR
<%= form_for([@user, @farm]) do |f| %>
应该有效。在前一种情况下,我们使用的命名路由与rake routes
输出中的POST路由匹配。在后一种情况下,我们会让rails根据您的@farm
对象的状态来计算出来。也就是说,如果@farm
是new_record?
,它会向/users/:user_id/farm
提交POST请求,或者@farm
是persisted?
,那么它就是/users/:user_id/farm
。 ll向{{1}}提交PUT请求。 (相同的路径,只是不同的请求类型。)
答案 1 :(得分:1)
一些问题:
-
<强> PARAMS 强>
在Rails中创建新的ActiveRecord对象时,您将要使用strong params
:
#app/controllers/farms_controller.rb
Class FarmsController < ApplicationController
def new
@farm = Farm.new
end
def create
@farm = Farm.new(farm_params)
end
private
def farm_params
params.require(:farm).permit(:params, :for, :farm)
end
end
-
<强>路线强>
您正在使用nested routing,如果您不习惯这可能会很棘手。
如果您还没有,您应该对您的路线执行此操作:
#config/routes.rb
resources :users do
resources :farm #-> /users/3/farm/new
end
这将使您能够从控制器或视图到达该路线。 如果你给我一些更多的信息(路线文件),我将能够进一步提供帮助!
-
发表强>
再次审视这个问题,似乎我在之前做了疏忽!!!
正如pdbobb
所指出的那样,错误肯定表示您尝试使用/new
动词来POST
。这是不正确的,因为根据Rails资源路由约定,您需要
您可以使用pdobb
的答案,但更重要的是,我们需要建立 您的表单尝试提交给{{ 1}}发帖。
问题可能来自您的new
资源