读书人

菜鸟提问:麻烦帮小弟我看看这个请求提

发布时间: 2013-01-01 14:04:18 作者: rapoo

初学者提问:麻烦帮我看看这个请求提交后是那个action处理
初学ror 路由规则看的不是很明白,只是知道请求如果没有指定的action 则默认是该控制器的 index 这个action去处理
那么有一个问题 在控制器里 没有index 这个action 然后又没有明确指定action 它是怎么去找到对应的action的,routes.rb里也看了 看不明白。下面是具体代码
1:提交的表单:
<form id="new_authentication" class="new_authentication" method="post" action="/authentication">

2:控制器authentication 中的 action 只有3个:def new def create def destroy

3:routes.rb 中的配置(只复制了1个 只有这个和authentication 有关系):
map.home "", :controller => "home", :action => "index"
map.resource :authentication

那么我的问题是:表单提交后 应该是去找authentication这个控制器 因为没有明确指出那个action 所以默认应该是找他的index 这个action,但是 这个控制器中没有index 这个action,那么它应该找那个action??(程序中是走的 create 这个 ) 但是我不明白为什么?请大师们指点 小弟
[解决办法]
hi dude,
do you know REST in the rails.
in the routes.rb file, you can see the authentication is a resource, so rails will automatically match these urls.
GET /authentications index
GET /authentications/id show
GET /authentications/new new
GET /authentications/id/edit edit
POST /authentications create
PUT /authentications/id update
DELETE /authentications/id destroy
we know http protocol defines 4 methods, they are get, post, put and delete.
these method will tell the server to get,create or delete a web resource.

map.resource :authentication this line will automatically generate some helper. such as authentications_path, authentication_path(id)......
more detail you can search RESTful in the rails.
also you check this web site, this is a rails tutorial
http://guides.rubyonrails.org/routing.html#resources-on-the-web

hope this is helpful.

[解决办法]
楼上说的是对的,ror采用RESTful, POST 对应的是create action

POST /authentications create

读书人网 >Ruby Rails

热点推荐