我想在集合上渲染一个部分,而不是使用一个 do 块来为我的集合中的每个单独实例渲染一个部分。
我读过Rails 指南,在我看来,部分应该正确渲染。
这是正在运行的实现:
index.html.erb
<ul class="tasks">
<% @days.each do |day| %>
<li class="<%= time_tense(day.date) %>">
<%= render "day", day: day %>
</li>
<% end -%>
</ul>
我想将上述代码块中的代码简化为:
<%= render "day", :collection => @days %>
这是我的部分内容(我意识到,对于正确的 HTML,我必须将所有内容包装在列表项中,但页面仍应显示某些内容,而不是返回错误......不是吗?):
_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 %>
但是,这不起作用,我很困惑为什么。我通过堆栈溢出进行搜索,我得到的结果是人们错误地使用了正在传递的集合的单数 var,而不是使用部分名称作为部分中的局部变量。由于我的部分被称为“天”,因此似乎并非如此。我在这里想念什么?
谢谢
编辑
这是应用程序跟踪:
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'
这是框架跟踪:
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'
编辑2:
如果我将@days 渲染到我的视图中,我会得到以下信息:
[#<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>]
我的“日”模型有两个属性:日期:日期和任务:文本。如上所示,有两种情况下任务为 nil。