2

I cannot figure out why I am getting this error. I know this question is very similar to this question, but I have all the jquery links correct as verified by the fact that I can delete users without a problem but not the microposts. When I try the example in the browser and click the delete link(http://localhost:3000/microposts/253) on a micropost, even though the item does get deleted the browser says:

Routing Error

No route matches [GET] "/microposts/253"
Try running rake routes for more information on available routes.

Test result:

Micropost pages micropost destruction as correct user should delete a micropost
     Failure/Error: expect { click_link "delete" }.should change(Micropost, :count).by(-1)
     ActionController::RoutingError:
       No route matches [GET] "/microposts/1"

routes.rb

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :microposts, only: [:create, :destroy]

  match '/signup', to: 'users#new'
  match '/signin', to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help', to: 'static_pages#help'
  match '/about', to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'

  root to: 'static_pages#home'...

Microposts delete link:

<%= link_to "delete", micropost, method: :delete,
                                 confirm: "You sure?",
                                 title: micropost.content %>

Microposts controller:

class MicropostsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  before_filter :correct_user,   only: :destroy

    def create
        @micropost = current_user.microposts.build(params[:micropost])
        if @micropost.save
          flash[:success] = "Micropost created!"
          redirect_to root_path
        else
          @feed_items = []
          render 'static_pages/home'
        end
      end

      def destroy
        @micropost.destroy
        redirect_back_or root_path
      end

      private

        def correct_user
          @micropost = current_user.microposts.find_by_id(params[:id])
          redirect_to root_path if @micropost.nil?
        end
end

I am unable to find the 3.2 tutorial repo to compare my sample_app to but I think I've followed the tutorial to the letter. Any help is appreciated.

Community
  • 1
  • 1
basheps
  • 10,034
  • 11
  • 36
  • 45

4 Answers4

1

I had the same problem, and after restarting the rails server it worked fine for me. [posting if anyone visits this page lately]

Aknahseh_tg
  • 158
  • 7
1

Yes, The problem is that you haven't updated your routes. Inside your routes.rb file, you have resources :microposts, only: [:create, :destroy]. The route that its looking for is a :show to :microposts.

Without seeing your controller code, I suspect that after you delete the micropost, you are trying to redirect back to the micropost. Either update your route to this: resources :microposts, only: [:create, :destroy, :show] or post up the details of your microposts controller.

TheDelChop
  • 7,938
  • 4
  • 48
  • 70
  • Just added the Micropost controller to the question. – basheps Feb 28 '12 at 14:49
  • hm... what was the solution? I'm having this issue, I initially tried adding show to the routes, but since I got the "Unknown action The action 'show' could not be found for MicropostsController" I came looking here. Also, I figure :show isn't necessary, as I'm issuing a delete request, right? – Laser Apr 26 '12 at 00:21
0

As I can see the problem is that your delete request goes as 'GET', But as per the REST conversions it should be a 'POST' request,

And I just created a sample app with Rails 3.1.3 and its delete link is as follows

In this case I have created a scaffold called User

<%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %>

You can check what is happening to your delete request by using Firebug with Firefox

sameera207
  • 16,547
  • 19
  • 87
  • 152
  • Delete link for the micropost is <%= link_to "delete", micropost, method: :delete, confirm: "You sure?" %> – basheps Feb 28 '12 at 14:51
0

I am working through the same tutorial, and am having the same problem. My analysis is that the problem is because we are using redirect_back_or root_path in the microposts controller (~/rails_projects/sample_app/app/controllers/microposts_controller.rb):

class MicropostsController < ApplicationController
...
  before_filter :correct_user,   only: :destroy
...
  def destroy
    @micropost.destroy
    logger.debug "in MicropostsController.destroy:"
    logger.debug " root_path=#{root_path}"
    logger.debug " session[:return_to]=#{session[:return_to]}"
    redirect_back_or root_path
  end

The output from the logger.debug statements shown is:

in MicropostsController.destroy:
 root_path=/
 session[:return_to]=/microposts/28

Recall that redirect_back_or is defined in sessions_helper.rb as:

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    clear_return_to
  end

So, the call to redirect_back_or root_path will result in:

redirect_to(/microposts/28)

I'm a newbie here, but I think the default action is GET; at least, that's consistent with what I'm seeing, i.e. this is why we are posting a GET to /microposts/28.

Meanwhile, in routes.rb, we have defined the resource microposts to only support create and destroy actions:

resources :microposts, only: [:create, :destroy]  

Of course, we don't want to GET the micropost we just deleted; we want to re-render (or re-direct?) back to the page from which we came. As evidence that this analysis is correct as far as it goes, I found that calling redirect_to root_path instead of redirect_back_or root_path "works", in that the micropost gets deleted (after popup confirmation), and puts the user back on the home page (showing the deleted micropost is gone).

I have now changed my as follows, so that the page from which the delete action was invoked re-appears:

  def destroy
    @micropost.destroy 
    redirect_to request.referer
  end

I also changed my definition of SessionsHelper#store_location:

  def store_location
    session[:return_to] = request.fullpath if ['show', 'edit'].include?action_name
  end

So, only 'show' and 'edit' actions attempt to resume after login.