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 Bee 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 Giacomoni wrote:

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

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

Laim Bee wrote:

Aldric Giacomoni 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 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 Giacomoni wrote:

Laim Bee wrote:

Aldric Giacomoni 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 <irb>:1:in 'new' from <irb>:1

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

Frederick Cheung 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 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.

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 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.<domainname>.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 Cheung wrote:

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.<domainname>.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?

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

Fred

Frederick Cheung wrote:

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

Colin

Colin Law wrote:

...

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

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

.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):

<h1> New Blog </h1> <% 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:

<h1> New Comment </h1> <% 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??

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?

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