On 30 March 2010 19:20, Stephen None <[email protected]> wrote:
> 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]])
>
>   �[email protected]_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"}

Look carefully at the params, you will see that you want
params[:book][:barcode] in your find.

You could have found this by using ruby-debug to break into your
controller where it is failing and inspecting the variables.  See the
guide on debugging.

I would have expected to see an id there also, are you not passing the
record up to the view where the form_for is?  Look at the code in the
controller#edit and view#edit and copy those except for missing out
the fields that you don't want in the view.  Then you should get the
id also and can lookup on that rather than the barcode, which would be
more conventional.  Unless I am misunderstanding exactly what you are
doing (which would not be unusual).

By the way, could you not 'top post' it would make it easier to follow
the thread.  Thanks.

Colin

>
> [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 <[email protected]> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to