Rails 3 上的 ambethia 的 reCAPTCHA 插件。覆盖 flash 消息 div 默认值?

发布于 2024-10-15 07:32:31 字数 860 浏览 8 评论 0原文

我在 Rails 3 上运行了 ambethia 的 reCAPTCHA 插件。有谁知道如何覆盖它的闪存消息标记?我想重复使用自己的 flash_error div id,而不是使用插件的 flash_recaptcha_error div id:

<div id="flash_recaptcha_error">incorrect-captcha-sol</div>

另外,您将如何清理此控制器#create?

def create
  @post = Post.new(params[:post])
  respond_to do |format|
    if verify_recaptcha(:model => @post, :error => "reCAPTCHA incorrect. Try again.") && @post.save
      flash.now[:notice] = "Created \"#{@post.title}\""
      format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
    else
      flash.now[:error] = "Incorrect word verification. Are you sure you\'re human?"
      format.html { redirect_to(:back, :error => 'reCAPTCHA incorrect. Try again.') }
    end
  end
end

感谢您阅读我的问题。

I have ambethia's reCAPTCHA plugin on Rails 3 working. Does anyone know how to override it's flash message markup? I'd like to reuse my own flash_error div id instead of using the plugin's flash_recaptcha_error div id:

<div id="flash_recaptcha_error">incorrect-captcha-sol</div>

Also, how would you clean up this controller#create?

def create
  @post = Post.new(params[:post])
  respond_to do |format|
    if verify_recaptcha(:model => @post, :error => "reCAPTCHA incorrect. Try again.") && @post.save
      flash.now[:notice] = "Created \"#{@post.title}\""
      format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
    else
      flash.now[:error] = "Incorrect word verification. Are you sure you\'re human?"
      format.html { redirect_to(:back, :error => 'reCAPTCHA incorrect. Try again.') }
    end
  end
end

Thanks for reading my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2024-10-22 07:32:31

因为 flash[] 是一个数组,所以您可以删除其中的元素。当我们使用 recaptcha gem 时,flash 数组包含 recaptcha_error 元素,因此您只需使用以下命令删除该元素:
控制器内的flash.delete(:recaptcha_error)

例如:

if  verify_recaptcha(:model=>@object,:message=>"Verification code is wrong", :attribute=>"verification code") && @object.save
  #your code if succes
else
  flash.delete(:recaptcha_error)
  #your code if its fail
end

也许它可以帮助你。谢谢

Because flash[] is an array you could delete element inside it. When we use recaptcha gem, the flash array contain recaptcha_error element, so you just only delete this element with :
flash.delete(:recaptcha_error) inside your controller.

For example :

if  verify_recaptcha(:model=>@object,:message=>"Verification code is wrong", :attribute=>"verification code") && @object.save
  #your code if succes
else
  flash.delete(:recaptcha_error)
  #your code if its fail
end

Maybe it could help you. Thanks

独自←快乐 2024-10-22 07:32:31

如果您从头开始创建用户身份验证系统,您可能必须执行以下操作:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    respond_to do |format|
        if verify_recaptcha(:model => @user )
            if @user.save
                format.html { redirect_to root_url,  :notice => "You have Signed up!" }
            else
                format.html { render :new }
            end
        else
            flash.delete(:recaptcha_error)
            format.html { redirect_to( root_path , :flash => { :error => 'Please retry the two words of the reCaptcha' } ) }
        end
    end
  end
end

If you're making a User Authentication System from scratch, you may have to do something like this:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    respond_to do |format|
        if verify_recaptcha(:model => @user )
            if @user.save
                format.html { redirect_to root_url,  :notice => "You have Signed up!" }
            else
                format.html { render :new }
            end
        else
            flash.delete(:recaptcha_error)
            format.html { redirect_to( root_path , :flash => { :error => 'Please retry the two words of the reCaptcha' } ) }
        end
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文