Rails:由不正确的控制器操作处理的URL

时间:2014-03-28 03:35:18

标签: ruby-on-rails ruby ruby-on-rails-3 rails-routing

我有一个由错误的控制器操作处理的URL。在我的路线文件中,我有以下

post '/tweets' => 'tweets#create'

每当我发布到此网址时,它都由tweets#index处理。

Started POST "/tweets" for 127.0.0.1 at 2014-03-27 20:04:25 -0700
Processing by TweetsController#index as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CCN9rDQiSXkVK0sF/BGU19fIufJTpho/ocySPmu7Lsc=", "tweet"=>{"title"=>"sdfdsafdsa", "description"=>"sdfdsasdadsa", "user_id"=>"1"}, "commit"=>"Create Tweet"}
  Tweet Load (0.4ms)  SELECT `tweets`.* FROM `tweets` 
  Rendered tweets/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 42ms (Views: 40.6ms | ActiveRecord: 0.4ms)

我尝试重新启动我的rails服务器但它仍然无法正常工作。

以下是rake routes

的结果
tweeter: rake routes
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                  tweets        /tweets(.:format)              tweets#index
              tweets_new        /tweets/new(.:format)          tweets#new
                         POST   /tweets(.:format)              tweets#create
                    root        /                              tweets#index

1 个答案:

答案 0 :(得分:5)

这是因为您在其他post '/tweets' => 'tweets#create'路由之后定义了tweet

通过将此行移到其他tweet路线上方来修改您的路线配置:

# config/routes.rb
...
post '/tweets' => 'tweets#create'
resources :tweets, only: [ :index, :new ]
...