gpt4 book ai didi

ruby-on-rails - Rails 没有路由错误,而 rake routes 给出了路由

转载 作者:行者123 更新时间:2023-12-05 03:14:26 24 4
gpt4 key购买 nike

我正在处理 Rails 中的嵌套资源“农场”,我创建新农场的表单如下所示:

 <%= 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 %>

我在农场 Controller 中对应的"new"功能是:

 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

它可以很好地呈现表单,但是在我单击提交之后,它实际上尝试创建新的农场,我收到此错误:

 No route matches [POST] "/users/2/farm/new"

在我的佣金 route ,我清楚地显示了这条路线:

     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"}

最佳答案

问题是您的表单正在尝试向仅存在于 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| %>

<%= form_for([@user, @farm]) do |f| %>

应该可以。在前一种情况下,我们使用的命名路由与 rake routes 输出中的 POST 路由相匹配。在后一种情况下,我们让 Rails 根据您的 @farm 对象的状态来计算它。也就是说,如果 @farm 是一个 new_record?,它将向 /users/:user_id/farm 提交一个 POST 请求,或者如果 @farmpersisted? 然后它会向 /users/:user_id/farm 提交一个 PUT 请求。 (相同的路径,只是不同的请求类型。)

关于ruby-on-rails - Rails 没有路由错误,而 rake routes 给出了路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com