Ruby on Rails, :notice - ruby-on-rails

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.

Related

Rails mailer: create object and access the id of the object inside a mailer view template

I'm creating and object inside a controller:
def create
#item = Item.new(item_params)
if #item.save
respond_to do |format|
format.html { redirect_to index_path, notice: "Created"}
format.json { render :'shows/show', status: :created, location: #item }
end
ModelMailer.delay.new_post(#user)
else
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
this is the mailer method:
def new_post(user)
#user = User.find(user.id)
attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")
mail(:to => #follow.email,
:subject => "Created a new post")
end
I would like to add the item.id thats is created to the mailer so I can access it in the email view template. Any ideas on how to implement this?
Just pass it. I don't actually see where you save the record so presumably your create example is incomplete, but once you have that working you can do...
ModelMailer.new_post(#user, #item.id).deliver_later
Then on the mailer
def new_post(user, item_id)
#item_id = item_id
The #item_id will be available in the view.
And to fix your controller create method (which is still wrong), it should be...
def create
#item = Item.new(item_params)
if #item.save
respond_to do |format|
format.html { redirect_to index_path, notice: "Created"}
format.json { render :'shows/show', status: :created, location: #item }
ModelMailer.delay.new_post(#user, #item.id)
end
else
respond_to do |format|
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end

Forwarding post parameters in a redirect_to rails

In my rails app, I want the user to be able to select an option in a 'new' form, but if the option already exists, I want it to update the current option.
I have this so far in my create method:
def create
#cost = Cost.new(cost_params)
if Cost.exists?(:category => #cost.category, :option => #cost.option)
redirect_to action: 'update', id: Cost.where(:category => #cost.category, :option => #cost.option).first.id
else
respond_to do |format|
if #cost.save
format.html { redirect_to action: 'index', status: 303, notice: [true, 'Cost was successfully created.'] }
format.json { render json: #cost, status: :created, location: #cost }
else
format.html { render action: "new" }
format.json { render json: #cost.errors, status: :unprocessable_entity }
end
end
end
end
The problem is that it redirects me to, for example cost/9 url, which renders the show page. I want the id to send with the cost_params straight to the update method:
def update
#cost = Cost.find(params[:id])
respond_to do |format|
if #cost.update_attributes(cost_params)
format.html { redirect_to action: 'index', status: 303, notice: [true, 'Cost was successfully updated.'] }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #cost.errors, status: :unprocessable_entity }
end
end
end
Which should redirect to the index page.
is there any efficient way to do this?
And HTTP redirect always results in a GET request, not a POST request, so redirecting to update doesn't really make a lot of sense. That's not a Rails issue, that's just how HTTP works.
If you want to automatically update the relevant record, you have to do that from within the create action. The straightforward but lazy way would be to copy the code from update and paste it into if branch within create. The more correct way would be to extract the relevant part of update out into a separate, private method, and call that method from both create and update, something like:
def create
#cost = Cost.new(cost_params)
if Cost.exists?(:category => #cost.category, :option => #cost.option)
#cost = Cost.where(:category => #cost.category, :option => #cost.option).first
really_update
else
respond_to do |format|
if #cost.save
format.html { redirect_to action: 'index', status: 303, notice: [true, 'Cost was successfully created.'] }
format.json { render json: #cost, status: :created, location: #cost }
else
format.html { render action: "new" }
format.json { render json: #cost.errors, status: :unprocessable_entity }
end
end
end
end
def update
#cost = Cost.find(params[:id])
really_update
end
private def really_update
respond_to do |format|
if #cost.update_attributes(cost_params)
format.html { redirect_to action: 'index', status: 303, notice: [true, 'Cost was successfully updated.'] }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #cost.errors, status: :unprocessable_entity }
end
end
end

ruby on rails redirect_to parameter passage not working

My controller file looks like this:
class QuotesController < ApplicationController
def show
#quote = Quote.find(params[:id])
#popup = params[:popup]
respond_to do |format|
if #popup.present?
format.html { render layout: false }
else
format.html
end
format.json { render json: #quote }
end
end
def create
#quote = Quote.new(params[:quote])
respond_to do |format|
if #quote.save
format.html { redirect_to #quote, notice: "Quote was successfully created.", popup: "1" }
format.json { render json: #quote, status: :created, location: #quote }
else
format.html { render action: "errors", layout: false }
format.json { render json: #quote.errors, status: :unprocessable_entity }
end
end
end
end
If I visit http://localhost:3000/quotes/1?popup=1 -- the view correctly displays without application_layout
However, if I am coming from the CREATE action, it seems ?popup=1 is never being appended to the URL - and therefore the application_layout is displaying when it should not be
I thought that adding popup: "1" to the redirect_to line was supposed to pass a param via GET
Can anyone see what I am missing?
Thanks
Edit: tried this on my machine and it worked:
{ redirect_to quote_path(#quote, :popup => "1"), notice: "Quote was successfully created." }
Try #quote_url(:popup=>1) I guess it will work.

Rails 3 - Redirect_to / Render another controller

I have 2 controllers: Projects and Users. Both models have no relation at all.
When I create a new Project I want to redirect to the new User path after saving the new project, but all my tries give erros like missing template or stuff like that.
How can I get this working?
EDITED
My create method in Projects controller:
def create
#project = Project.new(params[:project])
respond_to do |format|
if #project.save
format.html { render (new_user_registration_path) }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end
end
You don't want to render new_user_registration_path, you want to redirect_to new_user_registration_path
you must use redirect_to instead render:
redirect_to new_user_registration_path
respond_to do |format|
if #project.save
format.html { redirect_to new_user_registration_path }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end

Rails - Controller :notice - Add a variable?

I'm green behind the ears, but had a basic question about modifying the scaffolding's :notice to add a variable. For example, rails created the following create method for me:
def create
#order = Order.new(params[:order])
respond_to do |format|
if #order.save
format.html { redirect_to(#order, :notice => 'Order was successfully created.') }
format.xml { render :xml => #order, :status => :created, :location => #order }
else
format.html { render :action => "new" }
format.xml { render :xml => #order.errors, :status => :unprocessable_entity }
end
end
end
What I'm looking to do is add a variable to :notice so that it would print specifically what order was created (or edited with the update method). I tried some basic things using such as passing <%= order.id %>,though I felt this seemed unnatural within the controller?
Is adding a dynamic value possible within this format of this scaffolding? Or is it against the convention.
I appreciate the help, sorry if this is very newbish.
Beestings are the preferred way to insert dynamic values into strings in ruby. So if you wanted #order.id in your :notice, you could do this:
def create
#order = Order.new(params[:order])
respond_to do |format|
if #order.save
format.html { redirect_to(#order, :notice => "Order id # #{#order.id} was successfully created.") }
format.xml { render :xml => #order, :status => :created, :location => #order }
else
format.html { render :action => "new" }
format.xml { render :xml => #order.errors, :status => :unprocessable_entity }
end
end
end

Resources