0

I would like to render a partial over a collection instead of using an a do block to render a partial for each individual instance within my collection.

I've read the rails guide on rendering and it appears to me that the partial should be rendering correctly.

Here is the implementation that is working:

index.html.erb

<ul class="tasks">
    <% @days.each do |day| %>
        <li class="<%= time_tense(day.date) %>">
            <%= render "day", day: day %>
        </li>
    <% end -%>
</ul>

I would like to reduce my code from the above block into this:

<%= render "day", :collection => @days %>

Here is my partial (I realize that for correct HTML I would have to wrap everything within a list item, but the page should still display something instead returning an error... no?):

_day.html.erb

<h2 id='<%= day.id %>'>
    <%= format_date(day.date) %>
</h2>

<% if day.tasks.blank? %>
    <p id="day_<%=day.id%>_tasks">No tasks specified</p>
<% else %>
    <p id="day_<%=day.id%>_tasks"><%= day.tasks %></p>
<% end %>

<%= link_to "Edit", edit_day_path(day), id: "edit_#{day.id}", remote: true %>

However, this isn't working and I'm confused as to why. I searched through stack overflow I'm getting results where people are incorrectly using a singular var of the collection being passed instead of using the partial name as the local variable within the partial. Being that my partial is called "day", this doesn't seem to be the case. What am I missing here?

Thank you

EDIT

Here is the Application Trace:

app/views/days/_day.html.erb:1:in `_app_views_days__day_html_erb__715751556721750711_22867740'
app/views/days/index.html.erb:3:in `_app_views_days_index_html_erb__3971023009418116457_15614320'

Here is the Framework Trace:

actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:237:in `render'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.2.13) lib/action_view/helpers/rendering_helper.rb:27:in `render'
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.13) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/maobster/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__1760750132782399537__process_action__3203337094087687451__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__2496683798206605485__call__3749602834402969562__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/home/maobster/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/maobster/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/maobster/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

EDIT 2:

If I render @days into my view I get the following:

[#<Day id: 40, date: "2013-08-09", created_at: "2013-08-01 22:58:08", updated_at: "2013-08-02 11:46:37", tasks: "9">, #<Day id: 39, date: "2013-08-08", created_at: "2013-08-01 22:58:08", updated_at: "2013-08-02 11:46:36", tasks: "8">, #<Day id: 38, date: "2013-08-07", created_at: "2013-08-01 22:58:07", updated_at: "2013-08-02 11:46:17", tasks: "ds">, #<Day id: 37, date: "2013-08-06", created_at: "2013-08-01 22:58:07", updated_at: "2013-08-02 11:46:15", tasks: "sd">, #<Day id: 36, date: "2013-08-05", created_at: "2013-08-01 22:58:07", updated_at: "2013-08-02 11:30:13", tasks: "">, #<Day id: 35, date: "2013-08-04", created_at: "2013-08-01 22:58:07", updated_at: "2013-08-02 11:29:45", tasks: "asdfasdfasdf\r\nsdfsdf\r\nsdf">, #<Day id: 34, date: "2013-08-03", created_at: "2013-08-01 22:58:07", updated_at: "2013-08-02 11:31:10", tasks: "why can't i add multiple lines...?\r\n\r\nwhy???!!!">, #<Day id: 33, date: "2013-08-02", created_at: "2013-08-01 22:58:06", updated_at: "2013-08-02 11:28:31", tasks: "asdfasdfsdf">, #<Day id: 32, date: "2013-08-01", created_at: "2013-08-01 22:58:06", updated_at: "2013-08-02 11:23:28", tasks: "D">, #<Day id: 31, date: "2013-07-31", created_at: "2013-08-01 22:58:06", updated_at: "2013-08-02 05:50:30", tasks: "<p>asdfsdf</p>">, #<Day id: 30, date: "2013-07-30", created_at: "2013-08-01 22:58:06", updated_at: "2013-08-01 22:58:06", tasks: nil>, #<Day id: 29, date: "2013-07-29", created_at: "2013-08-01 22:58:05", updated_at: "2013-08-02 09:36:00", tasks: "dfgdfgdfg">, #<Day id: 28, date: "2013-07-28", created_at: "2013-08-01 22:58:05", updated_at: "2013-08-02 06:25:51", tasks: "asdfasdfasdfasdfasdfasdfasdfasdfefasefasefsefsefsef...">, #<Day id: 27, date: "2013-07-27", created_at: "2013-08-01 22:58:05", updated_at: "2013-08-02 06:26:43", tasks: "edit from the regular text area">, #<Day id: 26, date: "2013-07-26", created_at: "2013-08-01 22:58:05", updated_at: "2013-08-01 22:58:05", tasks: nil>]

My "Day" model has two attributes: date:date and tasks:text. There are two instances in which tasks is nil, as shown in above.

Eugene Kim
  • 145
  • 1
  • 1
  • 7
  • Have you tried a simple `render @days`? – sevenseacat Aug 03 '13 at 03:41
  • Yes. I have edited to original post, showing what rendering @days in the view displays. I also explain the two attributes of my "Day" model. There are two instances in which "tasks" is nil. – Eugene Kim Aug 03 '13 at 03:50

2 Answers2

0

The reason for the error is day is nil in that partial. Specify the :as option when you call the partial like follows:

<%= render :partial => "day", :collection => @days, :as => :day %>

With this each instance of the collection @days will be passed as day local variable to your partial.

Reference documentation: http://guides.rubyonrails.org/layouts_and_rendering.html#local-variables.

Update:

For this to work you need to specify the :partial option as shown above. And you only need the :as option if you wanted to alias your days instances, so the :as option is optional in your case since you are sending @days collection and your partial is _day.html.erb.

vee
  • 38,255
  • 7
  • 74
  • 78
  • Hrm I'm still getting the same error. Isn't the :as option for local variable renaming? – Eugene Kim Aug 03 '13 at 03:40
  • @EugeneKim, Tested this after your comment and have updated my answer. Please see my update and code changes. Yes you are right about the `:as` option. – vee Aug 03 '13 at 06:09
  • As, yes this works! Thank you! The guide you linked reads: "Also note that explicitly specifying :partial is required when passing additional options such as :layout." Is this the reason why? – Eugene Kim Aug 03 '13 at 06:43
  • @EugeneKim, Take a read on the answer here: http://stackoverflow.com/questions/4402556/rails-confused-about-syntax-for-passing-locals-to-partials# – vee Aug 03 '13 at 08:31
0

Would be helpful to check your error stack and see which file and which line the error occurs, so we directly know when to look first

I would suspect the error comes from your day partial, at this line:

<h2 id='<%= day.id %>'>

If its true (please confirm by looking at the location of the error in your stack trace) then it means that day is nil, so it means that you have a nil item in your @days collection.

Also you can have a look at better_errors gem https://github.com/charliesome/better_errors which displays your code where the error raised in your browser's screen, its a good shortcut.

EDIT

Ok seen your additions. All seems ok, and yes from the collection you should not have nil, but its never a waste of time to double check.

Now I can see only one possibility: you are certainly using the same partial somewhere else. Are you using an Ajax function to add records to a nested form? Like a link_to_add_fields method?

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • By the error stack are you referring to "Application Trace", "Framework Trace", and "Full Trace"? I have edited the original post to include the Application Trace and the Framework Trace. – Eugene Kim Aug 03 '13 at 03:42
  • The application trace seems to suggest that the error is indeed coming from that line. If I have some days which are nil, why is it that going through the collection @days one by one and rendering a partial for each individual instance is working but directly rendering a partial for the entire collection is not? – Eugene Kim Aug 03 '13 at 03:46
  • As far as I can tell this partial is only being rendered from the index page and I don't think I have any nested forms. I used to but scrapped a nested model a while ago. @vinodadhikary 's suggestion to include the partial: declaration worked. The rails guide on rendering (found here [link](http://guides.rubyonrails.org/layouts_and_rendering.html#local-variables)) states that if an additional option is declared when rendering a partial, :partial must explicitly be specified. As for the ins and outs of why this happens is beyond me. =P – Eugene Kim Aug 03 '13 at 06:53
  • Ok, glad you found it – Benjamin Bouchet Aug 03 '13 at 07:21