2012-07-02 24 views
21

# Giải thích bối cảnh

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! :)" 

# Vấn đề

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

# config/routes.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 

Tôi hy vọng vấn đề này là cách tôi tạo tuyến đường. Tôi đã thử nhiều thứ khác nhau.tuyến Tuỳ chỉnh POST để tạo hành động không bắn lên

# _form.html.erb

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

Tôi đang sử dụng tiêu chuẩn form_for helper.

đường # Rake cho đàn

$ 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 

Chúng ta thấy rõ ràng một con đường cho POST/helpcenter mà bị ràng buộc với các tạo tác dụng của bộ điều khiển diễn đàn.

# Logs

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

Các bản ghi rõ ràng cho thấy tôi đang làm một POST on/helpcenter nhưng cho nó kích hoạt lên action index thay vì tạo ra hành động!

# Tôi đang làm gì sai?

puts "Thanks!" 

Trả lời

26

Tôi nghĩ yêu cầu khớp với tuyến đường forums đầu tiên của bạn vì bạn không chỉ định phương thức HTTP. Điều này sẽ làm việc:

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

Hoặc phiên bản tốc ký:

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

cái nhìn đầu tiên cho thấy một POST chống/helpcenter vượt qua quy tắc cho các diễn đàn # chỉ số trận đấu, được gặp đầu tiên, do đó là những gì bạn nhận được

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

gì về:

match "/helpcenter" => "forums#index", :via => :get, :as => :forums 
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum 
Các vấn đề liên quan