Help on Inserting a record to the db - ruby-on-rails

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?

Related

Display a flash message on the same view after submit

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

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 doesn't populate dynamic generated fields on error on posting

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).

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

Rails, Is there any way in my view use link_to to grab the contents of a specific field and place it in a ruby variable?

Basically I am trying to capture the value of the form field before it is saved to the database. Then I intend to use that value in my controller to update a specific field in the database,
using
#taskforms.update_attribute('notes',
$notes)
I need to do this because I know of no other way to update that that does not require the full record to be validated.
The suggestion below to use #taskforms.save(false) is really not what I was looking for optimally. However it could work. However having and issue to get it to work.
What I am currently using (that works with validations)
def myupdate
#taskforms = Taskforms.find(params[:id])
respond_to do |format|
if #taskforms.update_attributes(params[:taskforms])
#taskforms.update_attribute('edited_at', Time.new )
flash[:notice] = 'Note was successfully updated.'
format.html { redirect_to(:controller => "taskforms", :action => "displayedit", :id => #taskforms.id) }
format.xml { head :ok }
else
format.html { render :action => "displayedit" }
format.xml { render :xml => #taskforms.errors, :status => :unprocessable_entity }
end
end
end
However when I try the save(false) it doesn't save and triggers validations anyway
def myupdate
#taskforms = Taskforms.find(params[:id])
if #taskforms.save(false)
#taskforms.update_attribute('edited_at', Time.new )
flash[:notice] = 'Note was successfully updated.'
format.html { redirect_to(:controller => "taskforms", :action => "displayedit", :id => #taskforms.id) }
format.xml { head :ok }
else
format.html { render :action => "displayedit" }
format.xml { render :xml => #taskforms.errors, :status => :unprocessable_entity }
end
end
I have never used Save in the past just the default respond_to do |format| so suspect my code is incorrect.
#taskforms.save(false) will save your model without any validations if that is your main goal.

Resources