0
votes

Just getting started with Rails and I am having a problem defining a route. None of the documentation I can seems to resolve the issue and I am getting an undefined local variable or method error.

I need to click on a link and take a specific action, sortit. sortit does not have a view because it sorts mystuff objects and then redirects to the index page. When I use one of the predefined actions then in fact everything works. Of course none of the predefined actions are what I want to do.

my /config/routes.rb file:

match "/mystuff/sortit'", :controller => "mystuff", :action => "sortit"

resources :mystuff

my /app/controllers/mystuff_controller.rb file

class MystuffController < ApplicationController
....
  def sortit
     @mystuff.sort
     redirect_to_mystuff_path
  end
....
end

my /app/views/mystuff/index.html.haml file:

-#  This file is app/views/mystuff/index.html.haml
%h1 All My Stuff

%table#mystuff
  %thead
    %tr
      %th= link_to raw("Type"), sortit
 ....

As I said, when I replace sortit with a predefined action, then that action is executed. However sortit fails with this error:

undefined local variable or method `sortit' for #<#<Class:0x9997a10>:0x997c0f8>
app/views/mystuff/index.html.haml:7:in `_app_views_mystuff_index_html_haml__61272557_87671610'

So what have I missed and how do I get sortit to execute when clicking on the generated link for Type?

Thanks!

PS: My rake routes output:

mystuff GET /mystuff (.:format) {:action=>"index", :controller=>"mystuff"}

....

mystuff_sortit /mystuff/sortit (.:format) {:controller=>"mystuff", :action=>"sortit"}

....

So rake routes shows my route with the name mystuff_sorit - but aren't actions prepended to the object name? Shouldn't it be sortit_mystuff instead?

1
Of course mystuff above is actually mystuffs ....Kirt Undercoffer
I think you want to add a key to the route match line for :as => 'sortit', then call sortit_path or sortit_url instead of just sortit.Tony Pitale
@Thanatos - thanks - that didn't generate an error just when the index.html page is displayed although when I clicked on the sortit link it still fails. In routes.rb I added the :as in the routes - and sortit_url in index.html.haml. However where is this documented? :action=>"sortit" is specified. Why is :as needed at all? Also where is the _url "suffix" documented? <p>routes.rb:</p> <p>match "/mystuff/sortit'", :controller => "mystuff", :action => "sortit", :as=>"sortit" </p> <p> %th= link_to raw("Type"), sortit_url</p>Kirt Undercoffer
@Thantos - that change resulted in this change in the rake routes output: sortit /mystuff/sortit (.:format) {:controller=>"mystuff", :action=>"sortit"} - so where is the code generating actually documented in detail?Kirt Undercoffer
This does result in a url being generated for Type: <a href="/mystuff/sortit">Type</a> - but now an ActiveRecord::RecordNotFound error is being thrown and a show action is apparently being tried to execute:ActiveRecord::RecordNotFound in MystuffController#show Couldn't find Mystuff with id=sortit app/controllers/mystuff_controller.rb:5:in `show'Kirt Undercoffer

1 Answers

0
votes

Your action is sortit but your view is index

Change your action to index

redirect_to needs a space also, so use redirect_to mystuff_path... which will actually be my_stuffs_path (plural) which you can see from rake routes (at the command line, most useful) which you get from defining it as a resource in routes.