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
Related
I am using this file uploader example for Ruby on Rail.
I have this piece of code in my controller. And I need to have a :notice parameter somewhere, so when the file is uploaded the notice will be "You have uploaded a file", if there is a error then "Something went wrong"
def create
p_attr=params[:upload]
p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array
#upload = Upload.new(p_attr)
respond_to do |format|
if #upload.save
#upload.update_attributes(:user_id => current_user.id)
format.html {
render :json => [#upload.to_jq_upload].to_json,
:layout => false
}
format.json { render json: [#upload.to_jq_upload].to_json, status: :created, location: #upload }
else
format.html { render action 'new' }
format.json{ render json: {name:(#upload.upload_file_name).split(".").first ,error: #upload.errors.messages[:upload_file_name]}, :status =>422}
end
end
end
So, I need something like this:
format.html { redirect_to(#upload, :notice => "LALALALALALA") }
but I have no idea how to integrate the :notice into my code
Thanks in advance.
This is how you 'integrate' the notice to your responses
def create
p_attr=params[:upload]
p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array
#upload = Upload.new(p_attr)
respond_to do |format|
if #upload.save
#upload.update_attributes(:user_id => current_user.id)
format.html { redirect_to(#upload, :notice => "Success") }
format.json { render json: [#upload.to_jq_upload].to_json, status: :created, location: #upload }
else
format.html { render action 'new', :notice => "Failed" }
format.json{ render json: {name:(#upload.upload_file_name).split(".").first ,error: #upload.errors.messages[:upload_file_name]}, :status =>422}
end
end
end
When you say 'integrate', do you mean how I can use the value of notice in view or the controller method?
If so, you can just use params[:notice] to get the value in your view or the controller you are redirecting to.
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 send a :user_id to my index controller when it redirects:
format.html { redirect_to #employee_personal, notice: 'Personal was successfully updated.'}
In my index controller I have to send:
#user_id = #employee_personal.user_id
variable.
How can I send this? In the redirect I tried:
format.html { redirect_to #employee_personal(:user_id => #user_id), notice: 'Personal was successfully updated.' }
It is wrong.
format.html { redirect_to :action => :index, :user_id => #user_id }
is right.
How can I make that error right in that format?
Try:
format.html { redirect_to #employee_personal, user_id: #user_id, notice: 'Personal was successfully updated.'}
Also, you can specify the path with params, something like this:
format.html { redirect_to employee_personal_path(#employee_personal, user_id: #user_id), notice: 'Personal was successfully updated.'}
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!' }
Lets say I have an ArtcilesController with create action like following.
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to(#article, :notice => "Article created") }
format.json { render :show }
else
format.html { render :new }
format.json { render(:json => { :errors => #article.errors }, :status => :not_acceptable) }
end
end
end
The same action can be written like following also:
def create
#article = Article.new(params[:article])
if #article.save
respond_to do |format|
format.html { redirect_to(#article, :notice => "Article created") }
format.json { render :show }
end
else
respond_to do |format|
format.html { render :new }
format.json { render(:json => { :errors => #article.errors }, :status => :not_acceptable) }
end
end
end
Notice that in first example there is an if else block inside of a respond_to block and in second, there are two respond_to blocks inside of a single if else block.
Should I prefer one over other? If yes, any reasons why? Or is it just a matter of choosing a style and sticking with it?
Style only however you're only ever responding to one request and using routing logic in your controller according to your models.
def create
#article = Article.new(params[:article])
respond_to do |format|
format.html {
#article.save ? redirect_to(#article, :notice => "Article created") : render :new
}
format.json {
#article.save ? render(:show) : render(:json => { :errors => #article.errors }, :status => :not_acceptable)
}
end
end
respond_with in Rails 3 will DRY this up, for boilerplate code as above. It even handles rendering edit/new forms and error messages when validation fails.