gpt4 book ai didi

ruby-on-rails - 未启动创建操作的自定义 POST 路由

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

# 解释上下文

puts "I am learning Rails, building a simple forum application."
puts "I am pretty satisfied to where I got so far but routes... "
puts "...still figuring them out."
puts "Been 2 days trying all sorts of things."
puts "This is where I am now, and something is not working as expected."
puts "Any help/pointers would be appreciated! :)"

# 问题

puts "I want my forum's create path to be '/helpcenter' and not '/helpcenter/cat'."
puts "When I access the form to create a new forum and I hit submit, "
puts "the form post to '/helpcenter' correctly (firebuged)"
puts "but I get the index, not the create!"
puts "I even put debugger in my create action but it is not being called."

# 配置/路由.rb

scope "/helpcenter" do
resources :cat, :controller => "forums", :as => :forums do
resources :topics , :controller => "forum_topics", :as => :topics
resources :posts, :controller => "forum_posts", :as => :posts
end
end

match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum

我希望这个问题出在我创建路线的方式上。我尝试了很多不同的东西。

# _form.html.erb

<%= form_for(@forum) do |f| %>
....
<% end %>

我正在使用标准的 form_for 助手。

# 为论坛获取路由
$ CONTROLLER=forums rake routes
delete_forum GET /helpcenter/cat/:id/delete(.:format) forums#delete
forums GET /helpcenter/cat(.:format) forums#index
POST /helpcenter/cat(.:format) forums#create
new_forum GET /helpcenter/cat/new(.:format) forums#new
edit_forum GET /helpcenter/cat/:id/edit(.:format) forums#edit
forum GET /helpcenter/cat/:id(.:format) forums#show
PUT /helpcenter/cat/:id(.:format) forums#update
DELETE /helpcenter/cat/:id(.:format) forums#destroy
forums /helpcenter(.:format) forums#index
create_forum POST /helpcenter(.:format) forums#create

我们清楚地看到 POST/helpcenter 的路由绑定(bind)到论坛 Controller 的创建操作。

# 日志
Started POST "/helpcenter" for 127.0.0.1 at 2012-07-02 12:25:00 -0400
Processing by ForumsController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"d5iVKCh234234=", "forum"=>{"name"=>"", "description"=>""}, "commit"=>"Save Changes"}

日志清楚地显示我正在/helpcenter 上进行 POST,但它会启动 index 操作而不是 create 操作!

# 我究竟做错了什么?

puts "Thanks!"

最佳答案

我认为该请求与您的第一个 forums 匹配路由,因为您没有指定 HTTP 方法。这应该有效:

match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum

或速记版本:
get "/helpcenter" => "forums#index", :as => :forums
post "/helpcenter" => "forums#create", :as => :create_forum

关于ruby-on-rails - 未启动创建操作的自定义 POST 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297623/

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