I have a very basic app with 1 controller that has new and create methods and a form_for to create an entry in the database with the passed params.
When i hit submit the show view is rendered. Is there a way not to have the page redirect, so the view stays still the same with the form and a flash message get displayed at the top of the view?
I tried flash[:notice] and flash.now[:notice] but no luck.
this is my create controller
def create
#campaign = Campaign.new(params[:campaign])
respond_to do |format|
if #campaign.save
flash[:notice] = "Success"
# format.html { redirect_to(#campaign, :notice => 'Successfully created.') }
# format.xml { render :xml => #campaign, :status => :created, :location => #campaign }
else
format.html { render :action => "new" }
format.xml { render :xml => #campaign.errors, :status => :unprocessable_entity }
end
end
end
Related
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
In our rails application we have a page where upon submit we save data in db. On this page, we have some fields which are dynamically generated and I see that in case of a validation error when page reloads it doesn't populate these fields with the values present upon posting.
In controller we have the following method defined for populating it:
def build_my_registration_type_memberships
#memberships = []
ListCache.my_registration_types.each do |my_registration_type|
#memberships << MyRegistrationTypeMembership.find_or_initialize_by_my_id_and_my_registration_type_id( #my.id, my_registration_type.id )
end end
In above method when my registration is opened in edit/view mode, it shows the values using this #membership method. But on posting in case of error it doesn't reload this with correct information. So my question is how could I repopulate #membership in case of an error on posting?
Thanks for help.
As, I understand you want some values available to your new method or the page that is rendered after if the create fails.
I assume you must have have the respond_to block in your create method. And you're doing this:
def create
...
respond_to do |format|
if #patient.save
format.html { redirect_to #object, :notice => "Object was successfully saved." }
format.xml { render :xml => #object, :status => :created, :location => #object }
else
format.html { render :action => :new }
format.xml { render :xml => #patient.errors, :status => :unprocessable_entity }
end
end
end
As you can notice, in the else part the new action is just rendered. Using some template the view is just delivered. Now, you just have to do whatever you're doing in the new action to make those values available, in the else part.
def create
...
respond_to do |format|
if #patient.save
format.html { redirect_to #object, :notice => "Object was successfully saved." }
format.xml { render :xml => #object, :status => :created, :location => #object }
else
format.html {
#memberships = []
ListCache.my_registration_types.each do |my_registration_type|
#memberships << MyRegistrationTypeMembership.find_or_initialize_by_my_id_and_my_registration_type_id( #my.id, my_registration_type.id )
end
render :action => :new
}
format.xml { render :xml => #patient.errors, :status => :unprocessable_entity }
end
end
end
And, the values you wanted will be available in the rendered form.
Better, you move to that code to a before filter or something, which makes those values available to those two methods (new and create).
I am just starting a rails 3 app that has a parent model and a child model (parent has_many :children).
I am trying to set things up so that after creating a new parent the user is taken to the show action of that parent (/parent/id). In this view I have included partials to show any children and a form to create a new child. After creating a new child the user is redirected to the show action for the parent where the new child will appear. This all works as intended.
However, if I try to validate fields in the new child form any error messages that occur are not appearing in the form (the necessary lines in the view are there and are correct - cut and pasted from generated scaffold code). Is there a way of passing these error messages for the child successfully to the parent show action?
Here are snippets of relevant code;
From my parent controller:
def show
#parent = Parent.find(params[:id])
#child = #parent.children.new
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #admission }
end
end
From my child controller:
def create
#child = Child.new(params[:parent])
respond_to do |format|
if #child.save
format.html { redirect_to(parent_path(params[:child][:parent_id]), :notice => 'Child was successfully created.') }
#This works as intended
format.xml { render :xml => #child, :status => :created, :location => #child }
else
format.html { redirect_to parent_path(params[:child][:patient_id]) }
#This redirects where I want it to go when validation fails but error messages are lost
format.xml { render :xml => #child.errors, :status => :unprocessable_entity }
end
end
end
Alright, I solved this myself. Thanks for Terw's answer though. It works apart from giving the wrong URL because of the lack of redirect.
It was simply a matter of passing the errors via the session hash and then adding them to the child model in the parent controller.
I'm posting the code because I couldn't find any other examples of this out there. Perhaps there's a simpler way but this works perfectly so far. If anyone thinks I'm crazy then please explain.
In my child controller
def create
#parent = Parent.find(params[:child][:parent_id])
#child = #parent.child.build(params[:child])
respond_to do |format|
if #child.save
format.html { redirect_to(parent_path(params[:admission][:parent_id]), :notice => 'Child was successfully created.') }
format.xml { render :xml => #child, :status => :created, :location => #child }
else
if #child.errors.any?
session[:child_errors] = #child.errors
end
format.html { redirect_to(parent_path(params[:child][:parent_id])) }
format.xml { render :xml => #child.errors, :status => :unprocessable_entity }
end
end
end
And in my parent controller
def show
#parent = Parent.find(params[:id])
#child = #parent.children.new
if session[:child_errors]
session[:child_errors].each {|error, error_message| #child.errors.add error, error_message}
session.delete :child_errors
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #parent }
end
end
What if you just render the parent's show page if the child is invalid?
def create
#child = Child.new(params[:parent])
respond_to do |format|
if #child.save
format.html { redirect_to(parent_path(params[:child][:parent_id]), :notice => 'Child was successfully created.') }
#This works as intended
format.xml { render :xml => #child, :status => :created, :location => #child }
else
format.html { render :controller => :parent, :action => :show }
# Display the parent's show page to display the errors
format.xml { render :xml => #child.errors, :status => :unprocessable_entity }
end
end
end
You might also want to do something like this for the crate action to make sure the parent exists.
#parent = Parent.find(params[:parent][:parent_id])
#child = #parent.children.build(params[:parent])
Then your users can't create an invalid child, and you have the parent to render the show page again. You shouldn't give the users the ability to set the parent_id them selfes.
I would like to insert a record into my db based on the values of querystring parameters, at the minute I just have the scaffold methods and it works, it takes the values at the in the new method, then when the sumbit button is clicked, it is inserted into the db.
How can I 'skip' the clicking of the button, so it is inserted just in the new method?
Code:
#book = Book.new(params[:book])
#book = Book.new({:user_id=>session[:user_id], :author=>session['test']})
respond_to do |format|
if #book.save
format.html { redirect_to(#book, :notice => 'Book was successfully created.') }
format.xml { render :xml => #book, :status => :created, :location => #book }
else
format.html { render :action => "new" }
format.xml { render :xml => #book.errors, :status => :unprocessable_entity }
end
end
Put this in the action which you want to do a save automatically:
#book = Book.new(params[:book])
#book.save
Some thing you might want to add: Only save if if params[:book] is set:
unless params[:book].blank?
#book = Book.new(params[:book])
#book.save
end
Also think of error handling. What if the book could not be stored?
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