1

因此,我遇到了一些 rspec 错误。我的数据库中有一个名称列,所以我不确定为什么这不起作用......错误读取 -

failures:

1) Sammiches GET /sammiches creates a new sammich
 Failure/Error: fill_in 'name', :with => 'lechuga'
 Capybara::ElementNotFound:
   Unable to find field "name"
 # ./spec/requests/sammiches_spec.rb:13:in `block (3 levels) in <top (required)>'

未注释掉的行失败。

1) Sammiches GET /sammiches creates a new sammich
 Failure/Error: visit sammiches_path
 ActionView::Template::Error:
   undefined method `sammich' for #<Sammich:0x007ff33b5594f8>

规格文件 -

require 'spec_helper'

describe "Sammiches" do
 describe "GET /sammiches" do
it "display some sammiches" do
    @sammich = Sammich.create :name => 'bacon'
    visit sammiches_path
    page.should have_content 'bacon'
end

it "creates a new sammich" do
    visit sammiches_path
    fill_in 'name', :with => 'lechuga'
    click_button 'create Sammich'

    current_path.should = sammich_path
    page.should have_content 'BLT'

    save_and_open_page
  end
   end
    end

我的看法是这样的。奇怪的是,如果我去掉那个评论部分,它只会“起作用”。对此的任何帮助也会很棒-

<h1>All my Sammiches</h1>
<%= form_for Sammich.new do |f| %>
<%= f.label :sammich %>
#<%= f.text_field :sammich %>#
<%= f.submit %>
<% end %>
  <ul>
<% for sammich in @sammiches %>
<li><%= sammich.name %></li>

<% end %>
 </ul>

谢谢!

页面来源

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Action Controller: Exception caught</title>
  <style>
    body { background-color: #fff; color: #333; }

    body, p, ol, ul, td {
      font-family: helvetica, verdana, arial, sans-serif;
      font-size:   13px;
      line-height: 18px;
    }

    pre {
      background-color: #eee;
      padding: 10px;
      font-size: 11px;
      white-space: pre-wrap;
    }

    a { color: #000; }
    a:visited { color: #666; }
    a:hover { color: #fff; background-color:#000; }
  </style>
</head>
<body>

<h1>
  NoMethodError in
  Sammiches#index
</h1>

<p>
  Showing <i>/Users/Cameron/sammy/sammiches/app/views/sammiches/index.html.erb</i> where line <b>#4</b> raised:
  <pre><code>undefined method `sammich&#x27; for #&lt;Sammich:0x007fac4169d300&gt;</code></pre>
</p>

<p>Extracted source (around line <b>#4</b>):
<pre><code>1: &lt;h1&gt;All my Sammiches&lt;/h1&gt;
2: &lt;%= form_for Sammich.new do |f| %&gt;
3: &lt;%= f.label :sammich %&gt;
4: &lt;%= f.text_field :sammich %&gt;
5: &lt;%= f.submit %&gt;
6: &lt;% end %&gt;
7: &lt;ul&gt;
</code></pre></p>

<p></p>


<p><code>Rails.root: /Users/Cameron/sammy/sammiches</code></p>

<div id="traces">
    <a href="#" onclick="document.getElementById(&#x27;Framework-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Full-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Application-Trace&#x27;).style.display=&#x27;block&#x27;;; return false;">Application Trace</a> |
    <a href="#" onclick="document.getElementById(&#x27;Application-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Full-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Framework-Trace&#x27;).style.display=&#x27;block&#x27;;; return false;">Framework Trace</a> |
    <a href="#" onclick="document.getElementById(&#x27;Application-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Framework-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Full-Trace&#x27;).style.display=&#x27;block&#x27;;; return false;">Full Trace</a> 

    <div id="Application-Trace" style="display: block;">
      <pre><code>app/views/sammiches/index.html.erb:4:in `block in _app_views_sammiches_index_html_erb__208297745638609695_70188865290640&#x27;
app/views/sammiches/index.html.erb:2:in `_app_views_sammiches_index_html_erb__208297745638609695_70188865290640&#x27;</code></pre>
    </div>
    <div id="Framework-Trace" style="display: none;">
      <pre><code>activemodel (3.2.13) lib/active_model/attribute_methods.rb:407:in `method_missing&#x27;
activerecord (3.2.13) lib/active_record/attribute_methods.rb:149:in `method_missing&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1159:in `value_before_type_cast&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1147:in `value_before_type_cast&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1041:in `block in to_input_field_tag&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1041:in `fetch&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1041:in `to_input_field_tag&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:690:in `text_field&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1284:in `text_field&#x27;
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:40:in `block in capture&#x27;
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer&#x27;
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:40:in `capture&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:378:in `form_for&#x27;
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument&#x27;
actionpack (3.2.13) lib/action_view/template.rb:143:in `render&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument&#x27;
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:45:in `render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:18:in `render&#x27;
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:36:in `render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:17:in `render&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:110:in `_render_template&#x27;
actionpack (3.2.13) lib/action_controller/metal/streaming.rb:225:in `_render_template&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:103:in `render_to_body&#x27;
actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body&#x27;
actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render&#x27;
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render&#x27;
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/benchmark.rb:295:in `realtime&#x27;
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime&#x27;
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render&#x27;
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:10:in `default_render&#x27;
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:5:in `send_action&#x27;
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action&#x27;
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__3460368674634880969__process_action__2573647100002742481__callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27;
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action&#x27;
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action&#x27;
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process&#x27;
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch&#x27;
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch&#x27;
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call&#x27;
journey (1.0.4) lib/journey/router.rb:68:in `block in call&#x27;
journey (1.0.4) lib/journey/router.rb:56:in `each&#x27;
journey (1.0.4) lib/journey/router.rb:56:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call&#x27;
rack (1.4.5) lib/rack/etag.rb:23:in `call&#x27;
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call&#x27;
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context&#x27;
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call&#x27;
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call&#x27;
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__2328833785069737987__call__85209834340556796__callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call&#x27;
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app&#x27;
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call&#x27;
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged&#x27;
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call&#x27;
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call&#x27;
rack (1.4.5) lib/rack/runtime.rb:17:in `call&#x27;
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call&#x27;
rack (1.4.5) lib/rack/lock.rb:15:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call&#x27;
railties (3.2.13) lib/rails/engine.rb:479:in `call&#x27;
railties (3.2.13) lib/rails/application.rb:223:in `call&#x27;
rack (1.4.5) lib/rack/content_length.rb:14:in `call&#x27;
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call&#x27;
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread&#x27;</code></pre>
    </div>
    <div id="Full-Trace" style="display: none;">
      <pre><code>activemodel (3.2.13) lib/active_model/attribute_methods.rb:407:in `method_missing&#x27;
activerecord (3.2.13) lib/active_record/attribute_methods.rb:149:in `method_missing&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1159:in `value_before_type_cast&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1147:in `value_before_type_cast&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1041:in `block in to_input_field_tag&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1041:in `fetch&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1041:in `to_input_field_tag&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:690:in `text_field&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:1284:in `text_field&#x27;
app/views/sammiches/index.html.erb:4:in `block in _app_views_sammiches_index_html_erb__208297745638609695_70188865290640&#x27;
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:40:in `block in capture&#x27;
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer&#x27;
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:40:in `capture&#x27;
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:378:in `form_for&#x27;
app/views/sammiches/index.html.erb:2:in `_app_views_sammiches_index_html_erb__208297745638609695_70188865290640&#x27;
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument&#x27;
actionpack (3.2.13) lib/action_view/template.rb:143:in `render&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument&#x27;
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:45:in `render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:18:in `render&#x27;
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:36:in `render_template&#x27;
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:17:in `render&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:110:in `_render_template&#x27;
actionpack (3.2.13) lib/action_controller/metal/streaming.rb:225:in `_render_template&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:103:in `render_to_body&#x27;
actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body&#x27;
actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render&#x27;
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render&#x27;
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/benchmark.rb:295:in `realtime&#x27;
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime&#x27;
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render&#x27;
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:10:in `default_render&#x27;
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:5:in `send_action&#x27;
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action&#x27;
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__3460368674634880969__process_action__2573647100002742481__callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27;
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#x27;
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument&#x27;
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action&#x27;
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action&#x27;
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action&#x27;
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process&#x27;
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process&#x27;
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch&#x27;
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch&#x27;
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call&#x27;
journey (1.0.4) lib/journey/router.rb:68:in `block in call&#x27;
journey (1.0.4) lib/journey/router.rb:56:in `each&#x27;
journey (1.0.4) lib/journey/router.rb:56:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call&#x27;
rack (1.4.5) lib/rack/etag.rb:23:in `call&#x27;
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call&#x27;
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context&#x27;
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call&#x27;
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call&#x27;
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__2328833785069737987__call__85209834340556796__callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks&#x27;
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call&#x27;
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app&#x27;
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call&#x27;
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged&#x27;
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call&#x27;
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call&#x27;
rack (1.4.5) lib/rack/runtime.rb:17:in `call&#x27;
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call&#x27;
rack (1.4.5) lib/rack/lock.rb:15:in `call&#x27;
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call&#x27;
railties (3.2.13) lib/rails/engine.rb:479:in `call&#x27;
railties (3.2.13) lib/rails/application.rb:223:in `call&#x27;
rack (1.4.5) lib/rack/content_length.rb:14:in `call&#x27;
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call&#x27;
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run&#x27;
/Users/Cameron/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread&#x27;</code></pre>
    </div>
</div>



<h2 style="margin-top: 30px">Request</h2>
<p><b>Parameters</b>: <pre>None</pre></p>

<p><a href="#" onclick="document.getElementById('session_dump').style.display='block'; return false;">Show session dump</a></p>
<div id="session_dump" style="display:none"><pre>_csrf_token: &quot;wsS7/d0sPVMEvPA2DIO1uJgLEhwH38/ckvq14iZZoxw=&quot;
session_id: &quot;a72797cacd094ecd4b6e57ece26e1ef2&quot;</pre></div>

<p><a href="#" onclick="document.getElementById('env_dump').style.display='block'; return false;">Show env dump</a></p>
<div id="env_dump" style="display:none"><pre>GATEWAY_INTERFACE: &quot;CGI/1.1&quot;
HTTP_ACCEPT: &quot;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&quot;
HTTP_ACCEPT_CHARSET: &quot;ISO-8859-1,utf-8;q=0.7,*;q=0.3&quot;
HTTP_ACCEPT_ENCODING: &quot;gzip,deflate,sdch&quot;
HTTP_ACCEPT_LANGUAGE: &quot;en-US,en;q=0.8&quot;
HTTP_CACHE_CONTROL: &quot;max-age=0&quot;
REMOTE_ADDR: &quot;127.0.0.1&quot;
REMOTE_HOST: &quot;localhost&quot;
SERVER_NAME: &quot;localhost&quot;
SERVER_PROTOCOL: &quot;HTTP/1.1&quot;</pre></div>


<h2 style="margin-top: 30px">Response</h2>
<p><b>Headers</b>: <pre>None</pre></p>



</body>
</html>
4

1 回答 1

2

I might get downvoted for this but it's difficult to explain on a comment. There is absolutely no need to do

<% for sammich in @sammiches %>
  <li><%= sammich.name %></li>
<% end %>

Instead you should do

<% @sammiches.each do |sammich| %>
  <li><%= sammich.name %></li>
<% end %>

As for why your code isn't working, the form_for tag helps you fill in your attributes of your record, inserts it into parameters, and sends it to the create action. But what really matters in your case is that you don't have an attribute named Sammich for the Sammich model. You do however have an attribute named name. So what do you do?

<%= form_for Sammich.new do |f| %>
  <%= f.label :sammich %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>
于 2013-04-29T01:08:19.807 回答