2009/8/7 bill walton <bwalton...@gmail.com>:
>
> Hi Doug,
>
>
> On Thu, 2009-08-06 at 18:34 -0700, doug wrote:
>> I have a problem of gut-wrenching proportions that can easily be
>> illustrated by the following snippit of template code:
>
> For starters, and please don't be offended, but other than the
> <p>...</p> line, none of this code belongs in your view template.
>
>> <% member=Member.find(params['id']) %>
>
> The find belongs in your controller or model and 'member' and its result
> should be passed 'up' to your view as an instance variable: @member.
>
> It's not possible to tell from the code you've posted, but there are
> lots of ways you can run into problems with the approach you're taking.
> The params hash is passed into your controller from a view.  It may or
> may not survive your controller/model processing for use in the next
> view's rendering.
>
> Also, the use of strings as keys in the params hash has been deprecated
> for some time; replaced with symbols: e.g., params[:id].  Also, the
> params hash contains strings.  The find method is looking for an
> integer.  So you'll be much safer using params[:id].to_i
>
>> <p>ID: <%= member.id %></p>
>
>>  <% outfile=File.open('/tmp/look1','w')
>>     outfile.puts(member.id)
>>     outfile.close %>
>
> The output to the file system definitely belongs in your controller or
> view.
>
>> The correct id is properly displayed within the paragraph element.
>
> This is most likely an accident.
>
>> However, the id that is sent to the disk file is incorrect in that,
>> for some unknown reason, it is the id of the very first record in the
>> database.  If I replace '.id' with '.inspect' in the 3ed to the last
>> line, the disk file confirms that it is actually accessing the first
>> record in the DB.  I can't figure out why that is.  It's like a bad
>> dream from which I expect to awake any moment.  I do hope that it
>> hasn't been a long day and that I'm doing something really stupid.
>>
>> Thanks for any input.
>
> I'd recommend you seperate the code above more appropriately.
>
> Try this for starters.  In your controller...
>
> def whatever
> �...@member = Member.find(params[:id].to_i)
>  outfile = File.open('/tmp/look1','w')
>  outfile.puts(@member.id)
>  outfile.close
> end
>
> Personally, for debugging purposes, I'd get rid of the file processing and 
> just to a puts to STDIO; e.g.:
>
> @member...
> puts "@member.id = #...@member.id}"
>
> It'll save you time.
>
> In your view, keep it to...
>
> <p>I saved a member record with ID = <%= @member.id %></p>
>

All of the above suggestions may be valid, however it does not explain
how, once the OP has managed by fair means or foul to get hold of the
member object that it manages to mutate between his two uses of it.

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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to