Rendering partial chokes on :locals

I am trying to render a partial and am getting a 500 error when I try to
pass variables in :locals.

-I created a new table using scaffolding
-I rake db:migrate my new table
-I copied the new.html.erb file and renamed it _new_note.html.erb in the
directory of the view that will be pulling it.
-I made sure to update the _new_note view to reference the variable
“note” instead of “@note” **

-In the View that I am trying to add the partial to, I added the
following code:
<%= render :partial=>“new_note”, :locals=>{:note=>Note.new} %>

In the _new_note partial, I put one line of text for testing. If I don’t
include " :locals=>{:note=>Note.new} " in my render statement, the text
will pull in fine. Otherwise, I will get a 500 error. Is there something
wrong with the Class (causing the object to not be created)? This works
ok on my computer using Mongrel, but doesn’t work on the production
environment for my real website. Any ideas? Thanks

**While I will eventually want note to be set, I deleted everything out
except for a line of text in the _new_note.html.erb file while
debugging.

Laim B. wrote:

-In the View that I am trying to add the partial to, I added the
following code:
<%= render :partial=>“new_note”, :locals=>{:note=>Note.new} %>

In the _new_note partial, I put one line of text for testing. If I don’t
include " :locals=>{:note=>Note.new} " in my render statement, the text
will pull in fine. Otherwise, I will get a 500 error.

That’s normal. When you create a partial / view and use a local variable
(like “note” for instance), then that variable needs to be passed to the
view. When rendering a partial, the way to pass in that variable is just
as you are doing.

You could also do something pretty ugly at the top of your partial
like…

<% note = note rescue Note.new %>
But, that is just a band-aid, and it’s pretty easy to lose track of
quick-fixes like that - what you are doing, passing in a new class, is
much closer to the behavior you want to emulate, so it’s better.

Aldric G. wrote:

Laim B. wrote:

-In the View that I am trying to add the partial to, I added the
following code:
<%= render :partial=>“new_note”, :locals=>{:note=>Note.new} %>

In the _new_note partial, I put one line of text for testing. If I don’t
include " :locals=>{:note=>Note.new} " in my render statement, the text
will pull in fine. Otherwise, I will get a 500 error.

That’s normal. When you create a partial / view and use a local variable
(like “note” for instance), then that variable needs to be passed to the
view. When rendering a partial, the way to pass in that variable is just
as you are doing.

You could also do something pretty ugly at the top of your partial
like…

<% note = note rescue Note.new %>
But, that is just a band-aid, and it’s pretty easy to lose track of
quick-fixes like that - what you are doing, passing in a new class, is
much closer to the behavior you want to emulate, so it’s better.

Do you know what would cause the entire page to not load properly then
and cause a 500 error? What does the rescue Note.new do?

Thanks,
Joe

Laim B. wrote:

Aldric G. wrote:

<% note = note rescue Note.new %>
But, that is just a band-aid, and it’s pretty easy to lose track of
quick-fixes like that - what you are doing, passing in a new class, is
much closer to the behavior you want to emulate, so it’s better.

Do you know what would cause the entire page to not load properly then
and cause a 500 error? What does the rescue Note.new do?

Keeping in mind that the proper thing to do is to pass the local
variable with the :locals hash, I’ll answer your question (and, in doing
so, I’ll realize I made a mistake!)
<% note = note rescue Note.new %>
That’s actually wrong, and if you did that, well, that’s why it’s
broken!
<% note = note || Note.new %>
That’s better.

You can check the behavior yourself with irb:
$ irb
irb(main):001:0> note
NameError: undefined local variable or method `note’ for main:Object
from (irb):1
from :0
irb(main):002:0> note = note
=> nil
irb(main):003:0> note
=> nil

The “rescue Note.new” was meant to tell Ruby “Should you encounter an
error with this assignment, create a new Note instead” – but clearly it
is not failing to assign.

Aldric G. wrote:

Laim B. wrote:

Aldric G. wrote:

<% note = note rescue Note.new %>
But, that is just a band-aid, and it’s pretty easy to lose track of
quick-fixes like that - what you are doing, passing in a new class, is
much closer to the behavior you want to emulate, so it’s better.

Do you know what would cause the entire page to not load properly then
and cause a 500 error? What does the rescue Note.new do?

Keeping in mind that the proper thing to do is to pass the local
variable with the :locals hash, I’ll answer your question (and, in doing
so, I’ll realize I made a mistake!)
<% note = note rescue Note.new %>
That’s actually wrong, and if you did that, well, that’s why it’s
broken!
<% note = note || Note.new %>
That’s better.

You can check the behavior yourself with irb:
$ irb
irb(main):001:0> note
NameError: undefined local variable or method `note’ for main:Object
from (irb):1
from :0
irb(main):002:0> note = note
=> nil
irb(main):003:0> note
=> nil

The “rescue Note.new” was meant to tell Ruby “Should you encounter an
error with this assignment, create a new Note instead” – but clearly it
is not failing to assign.

I tried pasting <% note = note || Note.new %> into my new Notes partial
and it still didn’t render.

I also tried creating a new Note in the production environment (using
ruby script/server production) and got the following error (See below).
It looks like a table doesn’t exist? Is it possible that when I
db:migrate 'd my table, it did it only to the development environment
and not the production environment?

scratch=Note.new
ActiveRecord::StatementINvalid:Could not find table ‘notes’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection)adapters/sqlite3_adapter.rb:29:in
‘table_structure’
from
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active)support/core-ext/object/misc.rb:39:
in ‘returning’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active)recrod/connection)adapters/sqlite3_adapter.rb:28:in
‘table_structure’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:213:in
‘columns’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1276:
in ‘columns’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:3008:
in ‘attributes_from_column_definition_without_lock’
from
/usr/lib/ruby/gems/1.8/gems/activerecord02.3.2/lib/active_record/locking/optimistic,rb:66:in
‘attributes_from_column_definition’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2435:
in ‘initialize’
from :1:in ‘new’
from :1

On Dec 21, 1:12 am, Laim B. [email protected] wrote:

In the _new_note partial, I put one line of text for testing. If I don’t
include " :locals=>{:note=>Note.new} " in my render statement, the text
will pull in fine. Otherwise, I will get a 500 error. Is there something
wrong with the Class (causing the object to not be created)? This works

Just to isolate things a little further, can you call Note.new
elsewhere ? (eg from script/console on your production machine) ?

Fred

ok on my computer using Mongrel, but doesn’t work on the production
environment for my real website. Any ideas? Thanks

**While I will eventually want note to be set, I deleted everything out
except for a line of text in the _new_note.html.erb file while
debugging.

Posted viahttp://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.

On Dec 28, 3:56 am, Laim B. [email protected] wrote:

I also tried creating a new Note in the production environment (using
ruby script/server production) and got the following error (See below).
It looks like a table doesn’t exist? Is it possible that when I
db:migrate 'd my table, it did it only to the development environment
and not the production environment?

did you do rake db:migrate RAILS_ENV=production (or setting RAILS_ENV
to production by another method) ? if not then you probably did only
run it against development

Fred

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.

On Jan 2, 5:21 pm, Laim B. [email protected] wrote:

Frederick C. wrote:

How does Rails know when you click the Submit button, that you want to
initiate the create method in the comments controller?

The conventions (in particular REST) that rails uses dictate that to
create a foo you post to /foos

Fred


Posted viahttp://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.

Frederick C. wrote:

On Dec 28, 3:56�am, Laim B. [email protected] wrote:

I also tried creating a new Note in the production environment (using
ruby script/server production) and got the following error (See below).
It looks like a table doesn’t exist? Is it possible that when I
db:migrate 'd my table, it did it only to the development environment
and not the production environment?

did you do rake db:migrate RAILS_ENV=production (or setting RAILS_ENV
to production by another method) ? if not then you probably did only
run it against development

Fred

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.

Thank you this helped. I won’t forget to actually migrate to production
again!

My site has a front page that shows an index of blog entries. I made a
new table to hold comments and my front page will now show the index
from my blog table, along with an index of comments (from the comment
table) and a new comment form (so visitors can just add a new comment on
the front page).

The New Comment form displays ok on the front page (I just took the new
view from the comments controller moved a copy of it into my blogs
view). I then rendered it as a partial. When I click on the “Create”
button, I get a 404 error. There’s nothing in the mongrel log or the
production log. I noticed that the toolbar now says
www..com/comments

I tried tweaking it thinking that it was not seeing the create method in
the comments controller, but this didn’t work.

How does Rails know when you click the Submit button, that you want to
initiate the create method in the comments controller?

Frederick C. wrote:

On Jan 2, 5:21�pm, Laim B. [email protected] wrote:

Frederick C. wrote:

How does Rails know when you click the Submit button, that you want to
initiate the create method in the comments controller?

The conventions (in particular REST) that rails uses dictate that to
create a foo you post to /foos

Fred


Posted viahttp://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.

So what’s the best way to debug why the create method is not working
properly? Is there a way to set a global variable that would be viewable
from ruby script/console?

2010/1/5 Laim B. [email protected]:

So what’s the best way to debug why the create method is not working
properly? Is there a way to set a global variable that would be viewable
from ruby script/console?

Have a look at the RoR guide on Debugging at

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

I also tried creating a new Note object directly in the partial itself
and this didn’t work. Is this the correct action that will occur when
you click the Post button:

Looks in Routes > Runs method from Controller > Resolves View

I have tried putting logger.debug in the create method of the Note
controller in hopes of proving that I am entering it. I doubt that I am
, but this is where I expect it to look. If I am using map.resources
:notes is this the method it should be looking at?

Colin L. wrote:

2010/1/5 Laim B. [email protected]:

So what’s the best way to debug why the create method is not working
properly? Is there a way to set a global variable that would be viewable
from ruby script/console?

Have a look at the RoR guide on Debugging at
http://guides.rubyonrails.org/

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

Thanks Colin. I took a look at the link (along with Head First Rails /
Simply 2 Rails and other websites) and I better understand why clicking
on the Submit button is redirecting to http://www..com/notes (It’s trying to perform a POST action…go REST!). I
tracked this to the create Method and have been playing with this method
thinking that the breakdown is happening here.

I tried using logger.debug since I believe the problem may exist in the
controller,(as mentioned here:
http://jeremyhubert.com/articles/debugging-in-rails.html) but wasn’t too
successful with it.

In the mean time, I fired up the console and manually created a new Note
object to verify that there wasn’t a problem with the model or my
database. I was able to create a new Note fine and display it on my
homepage with a partial that borrows from the index view of my Notes
object.

In my blogs “new” templtae, I noticed that I passed the action
explicitly with the following statement (blogs works great for me):

New Blog

<% form_for(@blog, :url=>{:action=>'create'}) do |f| %> . . etc

I tried passing the “create” action to my “new” note partial (which is
rendered in the index of my blog template) and the interesing thing is
that it redirected back to the “create” method of my blogs controller
and created a new blog entry! (Although it was blank because the columns
in the blogs table are different than the notes table). I thought all I
had to do was pass in the correct controller and then I would be golden:

New Comment

<% form_for(note, :url=>{:controller=>"Notes",:action=>'create'}) do |f| %> . . etc

Unfortunately this did not work out (There was no change in behavior).

I looked at the html source code for my home page to see if the form was
referencing the blogs controller since the note partial is on the blogs
template, but it did not look like it was. I checked it against the
source code for the same project on my local machine (where this partial
works) and the html source code for both forms look the same.

I think the problem may be related to the fact that I am using the
following line to render the partial:

<%= render :partial=>“new_note”, :locals=>{:note=>Note.new} %>

Could the problem be that when I submit the form, :note gets
overwritten??

=====================================
def create
@note = Note.new(params[:note])
@note.save
redirect_to ‘http://www.google.com

#respond_to do |format|
 # if @note.save
  #  flash[:notice] = 'Comment was successfully created.'
  #  format.html { redirect_to("http://www.google.com")}
  # format.xml  { render :xml => @note, :status => :created, 

:location => @note }
#else
# format.html { render :action => “new” }
# format.xml { render :xml=>@note.errors, :status=>
:unprocessable_entity }
#end
#end
end

====================================

Perhaps params[:note] gets set incorrectly? The odd thing is that I
can’t get the redirection to work and take me to google (Which I would
expect to happen if I was hitting the create method). This leads me to
believe that I’m not hitting the create method, however if this was the
case, why would it be trying to access ‘http:www..com/notes’? I would expect it to hit the controller and THEN try
to render the view.

I’m kind of out of ideas at this point of things to try. Since my
hosting service runs the webserver, I don’t see the server output like I
do when I run Mongrel from my desktop for testing. Also, there is no
information in the production log suggesting what may be causing the 404
error when I click the Submit button. I think it would be a good idea to
set a global variable as a marker to figure out for sure whether that
method is being hit.

Any ideas are appreciated. Thanks.

2010/1/16 Laim B. [email protected]:

I also tried creating a new Note object directly in the partial itself
and this didn’t work. Is this the correct action that will occur when
you click the Post button:

Looks in Routes > Runs method from Controller > Resolves View

Yes, though I would say renders view rather than resolves. So the
correct place to make the new note is in the create action, and
populate it from the posted params. Also remember that all the ruby
in the view is run on the server before the resulting html is sent to
the browser.

I have tried putting logger.debug in the create method of the Note
controller in hopes of proving that I am entering it. I doubt that I am
, but this is where I expect it to look. If I am using map.resources
:notes is this the method it should be looking at?

You should be able to see from development.log what is happening. You
can see all the params etc.
Sometimes ruby-debug is useful when you are not sure what is going on.
With ruby-debug you can simply put a break point in the create action
and check that it is getting there and what all the variables are set
to. Did I already point you to the rails guide on debugging?

Colin