当使用restfull路由处理带有错误消息的表单时,link_to_unless_current失败 [英] link_to_unless_current fails when processing forms with error messages in it with restfull routes

查看:95
本文介绍了当使用restfull路由处理带有错误消息的表单时,link_to_unless_current失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何防止link_to_unless_current的失败机制吗?

does anyone know how to prevent the failing mechanism of link_to_unless_current?

f.e .:我使用

link_to_unless_current "new task", new_task_path

当我单击链接时,我进入新的taks路径表单...并且未创建任何链接->确定. 然后,我在表格中输入了不正确的值并提交.

When I click on the link, i come to the new taks path form... And no link is created -> ok. Then I put incorrect values in the form and submit.

TasksController处理创建"操作,由于数据不正确,ActiveRecord-model的验证失败,并且控制器呈现新"操作(并包括模型的错误消息).

The TasksController processes the "create" action, the validation for the ActiveRecord-model fails because of the incorrect data and the controller renders the "new" action (and includes the error messages for the model).

class TasksController < ApplicationController
    def create
        @task = Task.new(params[:task])

        if @task.save
            flash[:notice] = 'task was successfully created.'
            redirect_to(tasks_url)
          else
            render :action => "new"
        end
    end
end

但是这里的链接被创建了! ->由于网址之间的差异:

But here the link gets created! -> Because of the difference between the urls:

  link path = new_task_path

但是

  posted path = tasks_path with :method => :post

有人知道如何彻底解决这个问题吗?

Does anybody know how to cleanly solve this problem?

谢谢

推荐答案

快速浏览link_to_unless_current ...

...它利用了current_path?,因此您应该能够执行以下操作:

...it makes use of current_path? such that you should be able to do something like this:

在助手中...

def current_page_in?(*pages)
  pages.select {|page| current_page?(page)}.compact.any? 
end

...然后在您看来,您可以像上面的Shadwell的答案一样,提供一个named_routes或哈希数组.

... and then in your view, you can just supply an array of either named_routes or hashes like Shadwell's answer above.

<%= link_to_unless(current_page_in?(new_thing_path, things_path), "add a thing") %>

您明白了...

已更新

对此进行了思考...如果您可以像希望原始方法那样工作就可以使用它,那就太好了.在这里,我们将提供的命名路由(或控制器+动作哈希)与当前页面及其引荐来源网址进行比较.

Had a think about this... and it'd be great if you could just use it like you'd hoped that the original method worked. Here we compare the supplied named route (or controller + action hash) with the current page AND its referrer.

def current_page_or_referrer_in(options)
  url_string = CGI.unescapeHTML(url_for(options))
  request = @controller.request
  # We ignore any extra parameters in the request_uri if the
  # submitted url doesn't have any either.  This lets the function
  # work with things like ?order=asc
  if url_string.index("?")
    request_uri = request.request_uri
    referrer_uri = request.referrer
  else
    request_uri = request.request_uri.split('?').first
    referrer_uri = request.referrer.split('?').first
  end

  #referrer_uri always has full path (protocol, host, port) so we need to be sure to compare apples w apples
  if url_string =~ /^\w+:\/\//
    ["#{request.protocol}#{request.host_with_port}#{request_uri}", referrer_uri].include?(url_string)
  else
    referrer_uri = referrer_uri.gsub(request.protocol, '').gsub(request.host_with_port, '')
    [request_uri, referrer_uri].include?(url_string)
  end
end

美妙之处在于,它现在使您可以执行此操作(根据您的示例):

The beauty is that it now lets you just do this (from your example):

<%= link_to_unless(current_page_or_referrer_in(new_task_path), "Add a task") %>

如果您是在new_task_path上,还是在其已发送到的页面(例如创建页面)上,它将显示出来

It'll then display if you're on new_task_path OR a page to which it has been sent (such as the create page

这篇关于当使用restfull路由处理带有错误消息的表单时,link_to_unless_current失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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