Nexus Weblogging
ChinaonRails
You are here ChinaonRails > Ruby on Rails > > Rails2.1中如何新加一个action方法?

蕲春人


0.291%

disconnected
登录后回复主题 | 切换简介显示 | 跳到回复(7)
Rails2.1中如何新加一个action方法?

Rails2.1中如何新加一个action方法?

... by 蕲春人 ... 5 月 5 天前 ... 279 次点击

Rails2.1中如何新加一个action方法?
对于新生成的Controller中,我自己定义了一个方法,然后通过url访问些方法,系统提示,说记录没有找到。
原来是在我调用/xxxs/login方法时,系统认为我是在调用xxxController的show方法,而login是作为show的参数。
有些郁闷。

我查了不少资料,应该是在routes.rb中配置resources之类的吧,具体的我没有搞定,我想你们没有遇到这个明显的问题吗?

1 - 6-19 15:48
bd7lx 深圳
routes.rb



ActionController::Routing::Routes.draw do |map|

map.xxxs 'xxxs',
:controller => "xxxs",
:action => "login"
end

2 - 6-19 21:17
markliu 地球
一个RESTful不应该包含login方法,你应该把对应的map.resources声明去掉,就会使用environment.rb最下面的map/:controller/:action/:id方式对应到你想要的login方法中了
3 - 6-23 13:04
CFC 臺灣
map.resources :xxxs, :collection => {:login => :get}
4 - 6-27 10:59
蕲春人 楼主 地球
我对marklin所说的做法很感觉,想知道具体地是如何做的.
5 - 6-27 19:27
蕲春人 楼主 地球
一个RESTful不应该包含login方法,你应该把对应的map.resources声明去掉,就会使用environment.rb最下面的map/:controller/:action/:id方式对应到你想要的login方法中了

不明白你第二句是什么意思.
你说不应该包含login方法?我的做法现在是再创建一个Login的controller,这样好不好呢?
6 - 6-27 19:28
蕲春人 楼主 地球
老外说的
Yeah, but why would you want to make something work that is so obviously wrong?

Think about it from a REST perspective: There's collections of resources (e.g. /users) and there's single items/members (e.g. /users/1). Where does the login action go? It obviously doesn't fit for the collection (you're trying to log in one users, not multiple) and it doesn't work for a member because at the moment of login you don't know which member (i.e. user) you're talking about. It just doesn't make any sense!
Also, take a look at the route you've created: login_new_user_path doesn't make sense - only existing users can log in!

Let me say it again: Have custom actions whenever they're necessary. If they're not necessary, leave them out.

Stuff that makes sense:
- bulk actions (e.g. deleting/activating/blocking/etc. multiple users) -> collection action that takes multiple user ids as params
- actions that change the state (e.g. activate/block/unblock/etc. a single user) -> member action

Of course, the implementation of these things doesn't necessarily have to be inside the User model, e.g. if you want users to block each other (e.g. private messaging), you might have a Block resource and internally call Block.create inside /users/block action.
7 - 6-27 20:43
robin 北京市海淀区
如果只是login的问题,看一下restful_authentication的做法吧。
如果是普通的加一个action方法,3楼已经回答得很明白了用:collection或者:member都可以。
看完之后有话想说?那就帮楼主加盖一层吧!

在回复之前你需要先进行登录
电子邮件或昵称
密码
© 2007 A Jesse Cai Production   -   About   -   京ICP备07020911号
a site powered by Project Babel