2012-03-19 57 views
0

刚开始使用Rails,我遇到了定义路由的问题。我似乎无法解决这个问题,我收到了一个未定义的局部变量或方法错误。Rails路由 - 未定义的本地变量或方法

我需要点击一个链接并采取特定的行动,排序。 sortit没有视图,因为它排序了mystuff对象,然后重定向到索引页面。当我使用预定义的操作之一时,实际上一切正常。当然,没有一个预定义的动作是我想要做的。

我/config/routes.rb文件:

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

resources :mystuff 

我/app/controllers/mystuff_controller.rb文件

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

我/app/views/mystuff/index.html.haml文件:

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

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

正如我所说的,当我与一个预定义的操作更换sortit,则执行该动作。然而sortit失败,此错误:

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' 

所以你有什么我错过了,我如何获得sortit到生成的链接类型上点击时执行?

谢谢!

PS:我的耙路线输出:

的MyStuff GET /的MyStuff(:格式){:行动=> “索引”,:控制器=> “的MyStuff”}

....

mystuff_sortit /的MyStuff/sortit(:格式){:控制器=> “的MyStuff”,:动作=> “sortit”}

....

所以耙路线显示我的路线Ť他将mystuff_sorit命名为 - 但不是对象名称的前缀操作?不是应该改为sortit_mystuff吗?

+0

当然上面的mystuff实际上是mystuffs .... – 2012-03-19 18:17:54

+0

我想你想添加一个键到路由匹配行:as =>'sortit',然后调用sortit_path或sortit_url而不是仅仅sortit。 – 2012-03-19 18:54:35

+0

@Thanatos - 谢谢 - 即使在显示index.html页面时也不会产生错误,但是当我点击sortit链接时仍然失败。在routes.rb中,我在路由中添加了:as - 并在index.html.haml中添加了sortit_url。但是,这在哪里记录? :action =>“sortit”被指定。为什么是:根据需要?还有_url“后缀”记录在哪里?

路线。RB:

匹配 “/的MyStuff/sortit”“,:控制器=> ”的MyStuff“,:动作=> ”sortit“:为=> ”sortit“

%TH =的link_to原始( ”类型“ ),sortit_url

2012-03-19 19:19:14

回答

0

你的行动是sortit,但你的观点是index

更改您的行动index

redirect_to需要的空间也,所以使用redirect_to mystuff_path ... 这实际上是my_stuffs_path(复数),你可以请参阅rake routes(在命令行中,最有用),将其定义为路由中的资源。

+0

sortit是一个真正的动作。它必须得到执行。它没有视图,因为它对mystuff列表进行排序,然后重新显示索引页。我编辑了原始帖子,前两行的耙路径输出 – 2012-03-19 18:33:17

+0

索引也是一个执行的动作。将排序添加到它是一个好主意(作为选项)。 – 2012-09-26 01:32:43

相关问题