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
Related
I have code in a Rails controller:
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
format.html { redirect_to invoice_path(#invoice), :notice => 'Invoice Time was successfully added.' }
else
format.html { redirect_to invoice_path(#invoice), :notice => 'ERROR.' }
end
end
end
end
Why am I getting the error too few arguments here:
format.html { redirect_to invoice_path(#invoice), :notice => 'Invoice Time was successfully added.' }
In the console, I get
>> invoice_path(#invoice)
=> "/invoices/29"
Thanks for your help!
You forgot to wrap the format. portion in a respond_to block. Should look like this:
respond_to do |format|
if newinvtime.save
format.html { redirect_to invoice_path(#invoice), :notice => 'Invoice Time was successfully added.' }
else
format.html { redirect_to invoice_path(#invoice), :notice => 'ERROR.' }
end
end
I've been trying to figure out this error for about 3 hours and have found other examples with same title, but didn't find any solution yet..
The website says:
syntax error, unexpected keyword_ensure, expecting $end, in /controllers/carts_controller.rb Line 17
respond_to do |format| /////// <-- says here the problem
The rails server says the problem is:
views/carts/show.html.erb:21: syntax error, unexpected keyword_ensure, expecting $end
...and here is the whole code:
/controllers/carts_controller.rb
class CartsController < ApplicationController
def index
#carts = Cart.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #carts }
end
end
def show
begin
#cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access ionvalid cart #{params[:id]}"
redirect_to store_url, :notice => 'Invalid cart'
else
respond_to do |format|
format.html
format.xml { render :xml => #cart }
end
end
end
def new
#cart = Cart.new
respond_to do |format|
format.html
format.xml { render :xml => #cart }
end
end
def edit
#cart = Cart.find(params[:id])
end
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render action: 'show', status: :created, location: #cart }
else
format.html { render action: 'new' }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
def update
#cart = Cart.find(params[:id])
respond_to do |format|
if #cart.update_attributes(params[:cart])
format.html { redirect_to(#cart, :notice => 'Cart was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #cart.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#cart = current_cart
#cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html {redirect_to(store_url, :notice => 'Your cart is currently empty') }
format.xml { head :ok }
end
end
end
indented all code
the view:
views/carts/show.html.erb
<div class="cart_title">Your Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', cart, :method => :delete, :confirm => 'Are you sure?' %>
SOLUTION FOUND:
The problem was on views/carts/show.html.erb. Changing cart to #cart solved the problem.
Thanks for the help.
I changed my form to remote, and while the form now works, the error messages are no longer displaying if there's an error.
<%= render 'shared/error_messages' %>
is there a good wear to get the messages to show again?
below is my controller...
thank you.
respond_to do |format|
if #post.save
format.js { render :js => "window.location = '#{edit_post_path #post}'" }
format.html { redirect_to [:edit, #post] }
else
format.js { render :js => #post.errors }
format.html { redirect_to '/', :error => "Could not save comment" }
end
end
respond_to do |format|
if #post.save
format.js { render :js => "window.location = '#{edit_post_path #post}'" }
format.html { redirect_to [:edit, #post] }
else
format.js { }
format.html { redirect_to '/', :error => "Could not save comment" }
end
end
# update.js.erb
$(document).find("form").prepend('<%= escape_javascript(render("shared/error_messages", :formats => [:html])) %>');
I am using twitter Boostraps tabbable feature found here:
http://twitter.github.com/bootstrap/components.html#navs
And within this navigation content window, I am trying to render a view that displays a "course". This view found in views/courses/_show.html.erb looks like this:
<div class="center hero-unit">
<h1><%= #course.course_name %></h1>
<%= link_to 'New Question Set',new_answer_path(:course_ID => #course.id), :class => "btn btn-large btn-primary" %>
<%= link_to 'Launch Session!', edit_course_path, :class => "btn btn-large btn-primary" %>
</div>
I am trying to render it and failing with the following code in views/instructor/show.html.erb
<% courses.each do |c| %>
<div class="tab-pane" id="<%=c.course_name%>">
<%= render :partial => 'courses/show', :locals => {#course=>c} %>
</div>
I get the following error:
/app/views/courses/_show.html.erb:1: syntax error, unexpected '=',
expecting keyword_end ...tput_buffer = #output_buffer; =
local_assigns[:];show = loca... ... ^
/app/views/courses/_show.html.erb:1: syntax error, unexpected ']',
expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or
tSTRING_END ...tput_buffer; = local_assigns[:];show =
local_assigns[:show];... ...
saying it's failing at line 1 of my courses/_show.html.erb
My Course Controller looks like this:
class CoursesController < ApplicationController
# GET /courses
# GET /courses.json
def index
#courses = Course.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #courses }
end
end
# GET /courses/1
# GET /courses/1.json
def show
#course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #course }
end
end
# GET /courses/new
# GET /courses/new.json
def new
#course = Course.new(instructor_ID: params[:instructor_ID])
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #course }
end
end
# GET /courses/1/edit
def edit
#course = Course.find(params[:id])
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(params[:course])
respond_to do |format|
if #course.save
format.html { redirect_to #course, :notice => 'Course was successfully created.' }
format.json { render :json => #course, :status => :created, :location => #course }
else
format.html { render :action => "new" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
# PUT /courses/1
# PUT /courses/1.json
def update
#course = Course.find(params[:id])
respond_to do |format|
if #course.update_attributes(params[:course])
format.html { redirect_to #course, :notice => 'Course was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
Note: I have ommited some of the methods like delete in my controller to save space.
Any Ideas?!
In #course=>c change the # to a colon.
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