在Rails上使用ambethia的reCAPTCHA插件3.覆盖flash消息div默认值?

时间:2011-01-31 03:15:47

标签: ruby-on-rails ruby-on-rails-3 controller ruby-on-rails-plugins recaptcha

我在Rails 3上运行了ambethia的reCAPTCHA插件。有谁知道如何覆盖它的flash消息标记?我想重用我自己的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

感谢您阅读我的问题。

2 个答案:

答案 0 :(得分:9)

因为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

也许它可以帮到你。感谢

答案 1 :(得分:0)

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

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