Question

I have a URL that is being processed by the wrong controller action. In my routes file I have the following

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

Whenever I post to this url it is instead processed by 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)

I have tried restarting my rails server yet it still doesn't work.

Here is the result of 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
Was it helpful?

Solution

That is because you have post '/tweets' => 'tweets#create' defined after other tweet routes.

Modify your route config by moving this line above other tweet routes:

# config/routes.rb
...
post '/tweets' => 'tweets#create'
resources :tweets, only: [ :index, :new ]
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top