在模型验证失败时使用自定义路由 [英] Use custom route upon model validation failure

查看:45
本文介绍了在模型验证失败时使用自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我的 Rails 应用程序中添加了一个联系表单,以便网站访问者可以给我发送消息.应用程序有一个 Message 资源,我定义了这个自定义路由以使 URL 更好、更明显:

I've just added a contact form to my Rails application so that site visitors can send me a message. The application has a Message resource and I've defined this custom route to make the URL nicer and more obvious:

map.contact '/contact', :controller => 'messages', :action => 'new'

当模型验证失败时,如何将 URL 保留为 /contact?目前,URL 在验证失败时更改为 /messages.

How can I keep the URL as /contact when the model fails validation? At the moment the URL changes to /messages upon validation failure.

这是我的 messages_controller 中的 create 方法:

This is the create method in my messages_controller:

def create
  @message = Message.new(params[:message])

  if @message.save
    flash[:notice] = 'Thanks for your message etc...'
    redirect_to contact_path
  else
    render 'new', :layout => 'contact'
  end
end

提前致谢.

推荐答案

一种解决方案是使用以下代码创建两条条件路由:

One solution would be to make two conditional routes with the following code:

map.contact 'contact', :controller => 'messages', :action => 'new', :conditions => { :method => :get }
map.connect 'contact', :controller => 'messages', :action => 'create', :conditions => { :method => :post } # Notice we are using 'connect' here, not 'contact'! See bottom of answer for explanation

这将使所有 get 请求(直接请求等)使用new"操作,而 post 请求使用create"操作.(还有另外两种类型的请求:put 和 delete,但这些在这里无关紧要.)

This will make all get request (direct requests etc.) use the 'new' action, and the post request the 'create' action. (There are two other types of requests: put and delete, but these are irrelevant here.)

现在,在您创建消息对象的表单中更改

Now, in the form where you are creating the message object change

<%= form_for @message do |f| %>

<%= form_for @message, :url => contact_url do |f| %>

(表单助手会自动选择 post 请求类型,因为这是创建新对象时的默认类型.)

(The form helper will automatically choose the post request type, because that is default when creating new objects.)

应该可以解决您的烦恼.

Should solve your troubles.

(这也不会导致地址栏闪烁其他地址.它从不使用其他地址.)

(This also won't cause the addressbar to flicker the other address. It never uses another address.)

.

  • 解释为什么这里使用connect不是问题map.name_of_route 只引用路径.因此,第二条路线不需要新的命名路线.您可以使用原始的,因为路径是相同的.所有其他选项仅在新请求到达 Rails 并且需要知道将其发送到哪里时使用.

.

编辑

如果您认为额外的路由有点混乱(尤其是当您更频繁地使用它时),您可以创建一个特殊的方法来创建它们.这种方法不是很漂亮(可怕的变量名),但它应该可以胜任.

If you think the extra routes make a bit of a mess (especially when you use it more often) you could create a special method to create them. This method isn't very beautiful (terrible variable names), but it should do the job.

def map.connect_different_actions_to_same_path(path, controller, request_types_with_actions) # Should really change the name...
  first = true # There first route should be a named route
  request_types_with_actions.each do |request, action|
    route_name = first ? path : 'connect'
    eval("map.#{route_name} '#{path}', :controller => '#{controller}', :action => '#{action}', :conditions => { :method => :#{request.to_s} }")
    first = false
  end
end

然后像这样使用

map.connect_different_actions_to_same_path('contact', 'messages', {:get => 'new', :post => 'create'})

不过我更喜欢原来的方法...

I prefer the original method though...

这篇关于在模型验证失败时使用自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆