简体   繁体   English

Ruby on Rails发布请求路由

[英]Ruby on rails post request routing

I am a newbie in the world of ruby on rails, and trying to find out how routing works. 我是轨道上的红宝石世界的新手,并试图找出路由的工作方式。 I read some articles about it but something is not clear to me. 我读了一些有关它的文章,但是我不清楚。
If I have a page, with a message sender form and try to send the data via post, I have to set the route sg like this: 如果我有一个带有消息发件人表单的页面,并尝试通过邮寄发送数据,则必须设置路由sg,如下所示:

post '/send', to: 'message#send'

with this it works fine. 与此一起很好。 But what if I have an another page with another form and I want to link it to another controller/action(post request too). 但是,如果我有另一个具有其他表单的页面,并且想要将其链接到另一个控制器/动作(也发送请求),该怎么办? How can It make a disctinction between the 2 posts? 如何区分这两个职位?

You can pass some special parameter and check it's value in controller, for instance: 您可以传递一些特殊参数,并在控制器中检查其值,例如:

class MessageController < ApplicationController
  def send
    if params[:kind] == 'some_value'
      do_one_thing
    else
      do_anoter_thing
    end
  end
end

But in this case your action will become fat and ugly. 但是在这种情况下,您的动作将变得肥胖而丑陋。 Thus I suggest you to create new action and separate logic in a natural way: 因此,我建议您以自然的方式创建新动作并分离逻辑:

post '/my_send_from_one_place', to: 'message#my_send_from_one_place'
post '/my_send_from_secong_place', to: 'message#my_send_from_secong_place'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM