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.
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
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
Related
I having difficulties with the #seller_profile.save statement. Why is it not saving?
def create
#seller_profile = SellerProfile.new(seller_profile_params)
#seller_profile.seller = current_seller
if #seller_profile.save
redirect_to home_index_path
else
puts "Error"
end
end
def current_seller
#current_seller ||= Seller.find(session[:seller_id]) if session[:seller_id]
end
You need to Hanlde your else part with Proper error condition handling
Simple Put will not do the Job
There are following options to handle it
1st by ony Render the JSON
if #seller_profile.save
redirect_to home_index_path
else
render json: #seller_profile.errors.to_json.to_json
end
2nd redirect back with error in notification
if #seller_profile.save
redirect_to home_index_path
else
flash[:error] = #restaurant.errors
redirect_to :back
end
3rd Option to use both Option of Render and Redirect at Same Time
if #seller_profile.save
redirect_to home_index_path
else
respond_to do |format|
format.html { redirect_to :back , flash[:error] = #restaurant.errors }
format.json { render json: #seller_profile.errors.to_json.to_json }
end
end
I have a Rails page that lets the user select one or more records.
This is the controller action:
def addinvtimes
#invoice = params[:invtimes][:invoice_id]
if params[:event_ids] != nil
params[:event_ids].each do |i|
newinvtime = Invtime.new(
linetype_id: 1,
invoice_id: #invoice,
event_id: i.to_i
)
if newinvtime.save
respond_to do |format|
if newinvtime.save
format.html { redirect_to invoice_path(#invoice), :notice => 'Invoice Item was successfully added.' }
else
format.html { redirect_to invoice_path(#invoice), :notice => 'ERROR.' }
end
end
end
end
end
end
There error I get is:
Render and/or redirect were called multiple times in this action
How can I code to not call the redirect multiple times?
Your actual problem is not in the double/redirect error, but in the logic. Assume the params[:event_ids] contain 1000 items. Is where it should to be redirected?
params[:event_ids].each do |i|
if something.save
redirect_to .....
else
redirect_to .....
end
end
You can redirect only once at an action call, but you trying to call redirect in each step of the iterator, I am pretty sure you can't do this.
This is in the login action. I'm trying to adapt it to send back a return_url query param, and i'm failing with a simple render.
Prior:
respond_to do |format|
format.html {
flash.now[:error] = error
render
}
Attempt 1:
respond_to do |format|
format.html {
flash.now[:error] = error
render return_url => "blah"
}
Attempt 2:
respond_to do |format|
format.html {
flash.now[:error] = error
render "login", local => {return_url => "blah"}
}
I passed params[:redirect_after_error] after an error and that solved it.
respond_to do |format|
format.html {
flash.now[:error] = error
params[:redirect_after_error] = params[:return_url]
render
}
From what I understand. You are trying to pass a return_url to the view. Right?
So if you want that you can just create a instance variable that contains the value of the returning url
For ex
#return_url = root_path() #or change it to url you want
#Then follow with the code you written
#You will see that you can use #return_url in the view without doing anything else.
If it doesn't answer your question. Could you be precise. I am not so sure I do understand your question correctly.
for this view should have one instance variable when its render, so if its fail you just render to root_path
ex:
#user = xxxx respond_to do |format|
if #user.save
format.html { redirect_to #users, notice: 'user was successfully created.' }
else
format.html { render root_path, :alert: error }
end
end
I have such code in controller:
if verify_recaptcha #verify_recaptcha(:model => #order, :message => "Oh! It's error with reCAPTCHA!") &&
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
OrderMailer.order_confirmation(#order, #user).deliver
UserOrderMailer.user_order_mailer(#order, #user).deliver
#OrderNotifier.received(#order).deliver
pdf_link = generate_pdf(#order)
link = " #{I18n.t(:get_pdf)}"
format.html { redirect_to root_url, :notice => (I18n.t(:successful_order_send)+link.to_s).html_safe}
else
format.html { render :action => "new", :notice => I18n.t(:error) }
format.xml { render :xml => #order.errors, :status => :unprocessable_entity }
end
end
else
flash[:warning] = I18n.t(:wrong_captcha)
redirect_to :back
end
And in form view:
- if #user_vehicle.errors.messages.values.present?
.warning
- #user_vehicle.errors.messages.values.each do |msg|
- msg.each do |m|
%li= m
But also in layout i have:
- if flash[:warning]
.warning
= flash[:warning]
- if flash[:notice]
.notice
= flash[:notice]
I want to know, how can i append recaptcha fail error to errors.messages.values list of error's and display in same div and with li as i do for model validation messages?
How can i see as one more li item recaptcha message?
if verify_recaptcha
...
else
#user_vehicle.errors.add(:base, I18n.t(:wrong_captcha))
end
I want to redirect_to slider_path after a user submits their email. Currently, only the success message is displayed without a redirect. Here's the code:
class Splash::SubscribersController < ApplicationController
def create
#subscriber = Subscriber.new(params[:subscriber])
if #subscriber.save
success = true
message = "Success! We'll let you know when we launch."
else
success = false
message = "Fail."
end
respond_to do |format|
format.html {
if success
flash[:success] = message
redirect_to slider_path
else
flash[:error] = message
end
redirect_to root_path
}
format.json { render :json => { :success => success, :message => message }.to_json }
end
end
end
Just replace this part of your code:
if success
flash[:success] = message
redirect_to slider_path
else
flash[:error] = message
end
redirect_to root_path
with this:
if success
flash[:success] = message
redirect_to slider_path
else
flash[:error] = message
redirect_to root_path
end
Rails API states:
An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError:
def do_something
redirect_to :action => "elsewhere"
render :action => "overthere" # raises DoubleRenderError
end
If you need to redirect on the condition of something, then be sure to add “and return” to halt execution.
def do_something
redirect_to(:action => "elsewhere") and return if monkeys.nil?
render :action => "overthere" # won't be called if monkeys is nil
end
Note the use of and return
Neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
Add a return statement after your redirect. If the action also renders a template by default, any redirects need to be followed by a return statement.
if success
flash[:success] = message
redirect_to slider_path
return # <= Add a return.
else
flash[:error] = message
end
redirect_to root_path