rubyonrailsin

A Ruby and Rails talk

Tuesday, March 30, 2010


[Rails] Re: Re: Updating records

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

I switched over to form_for as you suggested and that seems to be
working much better but I am still running into a problem. The form
appears to be submitting the correct data but I am having a problem with
selecting the record correctly. Specifically, in my controller,

def update

@book = Book.find(:first, :conditions => ["barcode =
?",params[:barcode]])

@book.update_attributes(params[:book])
redirect_to "/books"

end

If I replace the params[:barcode] with a real barcode (e.g.
Book.find(:first, :conditions => {'barcode' => ['123456789']})) number
the form processes successfully and updates my database. When I try
from the form however I am getting a null value error even though I can
see the parameters being passed along in the logs so I am not sure why
they are not available to the model...

Processing BooksController#update (for 10.0.1.65 at 2010-03-30 14:01:07)
[POST]
Parameters: {"commit"=>"Update", "action"=>"update",
"authenticity_token"=>"vq6j9DJ57+AP4kbdbFHjeqeXHiYJDr9RT+zwHVCJ7mI=",
"book"=>{"bounceback"=>"1", "barcode"=>"123456789"},
"controller"=>"books"}

[4;36;1mBook Load (0.6ms)[0m [0;1mSELECT * FROM `books` WHERE (barcode
= NULL) LIMIT 1[0m

I have tried every combination of :conditions I can think of but I don't
seem to be able to get it to work correctly. Thanks!

Stephen

Colin Law wrote:
> On 29 March 2010 21:44, Stephen None <lists@ruby-forum.com> wrote:
>> the update page rather than just processing the update method.  I know
>> <p>
>>  <%= submit_tag "Update" %>
>> </p>
>> <% end -%>
>
> As I said it is easier to use the form_for method. Just copy the
> existing edit view code (which presumably includes the barcode and
> checkout values) and remove from the form the fields that you do not
> want.
>
> The problem with the submit is that you are not putting the data in
> the params correctly. Try doing an edit and look in development.log
> to see what the params are, then do the same with yours and compare
> them. Your fields should be inside a hash for the complete object.
> But as I said if you use form_for it will all be done for you. I
> don't understand what problem you are having with form_for, what has
> it got to do with loading the edit page? You are using your new page
> instead of the edit page in this case.
>
> Colin

--
Posted via http://www.ruby-forum.com/.

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