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
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.
I need the block respond_to not render to new.html.erb if not a another view created by me called for example new_form.html.erb
def new
#user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user }
end
end
Pretty simple. As long as the view is in the default directory for the controller:
respond_to do |format|
format.html render 'new'
format.json { render json: #user }
end
If not, you need to tell it which directory:
respond_to do |format|
format.html render 'users/new'
format.json { render json: #user }
end
More docs here: http://guides.rubyonrails.org/layouts_and_rendering.html
There are many ways to do it....
##FOR HTML CALLS
format.html { render 'new'}
format.html { render 'shared/new'}
##FOR JS CALLS
format.js { render 'new'}
format.js { render 'shared/new'}
##pass variable to the view
format.js { render 'shared/new',:locals=>{:type=>"User"}}
##OR you can also try redirect in some rare cases WITHOUT respond_to block
redirect_to users_path(params[:id])
I just want to flash an notice/error if the message is/isn't saved, without any redirect, how can I have no redirect:
respond_to do |format|
if #message.save
format.html { redirect_to request.referer, :notice => 'Message sent!' } #dont want redirect
else
# error message here
end
Use flash.now:
if #message.save
flash.now[:notice] = 'Message sent!'
else
flash.now[:alert] = 'Error while sending message!'
end
respond_to do |format|
format.html { # blahblah render }
end
In rails 5, you can do:
format.html { redirect_to request.referer, alert: 'Message sent!' }
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