rubyonrailsin

A Ruby and Rails talk

Monday, March 29, 2010


Re: [Rails] Updating records

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

On 29 March 2010 19:00, Stephen None <lists@ruby-forum.com> wrote:
> So I would like to be able to update a record without having to load the
> edit page.  Lets use checking out a book as an example.  From the front
> page of my application I would like to be able to have a form field
> where I can enter a record id and select one of a couple of check boxes
> and then hit submit which will update the associated record with the
> action selected by the check box (check out, check in, etc).  I am
> assuming I need to use a form_tag but I am not sure how to go about
> writing the controller and I am assuming I will need to do some custom
> routing.
>
>  def update
>
>    @barcode = params[:barcode]
>    @checkout = params[:checkout]
>
>    @book = Book.find(:first, :conditions => {:barcode => [@barcode]})
>
>    @book.update_attributes(params[:book])
>
>  end

Just use the same technique as on the edit page but miss out the
fields that you do not wish to change. Then submit to the existing
update action, you do not need to change it as update_attributes only
changes the fields that are provided, and leaves the others unchanged
(I think that is correct, someone will correct me if I am wrong).
Depending on how similar the required view is to the normal edit view
you may even be able to use the same one, just hiding the fields you
do not want.

By the way form_for is the conventional way now, rather than form_tag.
Your edit view will likely be using that unless you picked up the
code from an old tutorial or book.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive