问题 Rails form_tag表单写入 - 具有非活动记录模型


我有点像Rails的新手。我正在写一个couchrest-rails应用程序,所以我没有使用activerecord这个模型。我只是想通了那就意味着

form_for(@model) 

不行。我正在尝试研究如何使用form_tag - 但大多数示例都不涉及new和create操作。

这是错的:

<h1>New article</h1>

<% form_tag new_article_url(@article), :method => :post do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', articles_path %>

因为当我运行我的Cucumber场景时,我得到了这个:

Scenario: Create Valid Article                            # features/article.feature:16
  Given I have no articles                                # features/step_definitions /article_steps.rb:8
  And I am on the list of articles                        # features/step_definitions/webrat_steps.rb:6
/home/deploy/www/www.trackingplace.com/app/ccc/app/views/articles/new.html.erb:3: warning: multiple values for a block parameter (0 for 1)
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/helpers/capture_helper.rb:36
When I follow "New Article"                             # features/step_definitions/webrat_steps.rb:18
  You have a nil object when you didn't expect it!
  The error occurred while evaluating nil.error_messages (ActionView::TemplateError)
  features/article.feature:19:in `When I follow "New Article"'

但我不明白错误,或如何解决它。


9434
2017-08-13 16:57


起源



答案:


form_tag 方法不使用表单生成器,因此您不能在表单块中使用“f”变量。代替 f.error_messages 你必须使用 error_messages_for

<% form_tag new_article_url(@article), :method => :post do %>
  <%= error_messages_for :article %>

  <p>
    <%= label :article, :title %><br />
    <%= text_field :article, :title %>
  </p>
  <p>
    <%= submit_tag 'Create' %>
  </p>
<% end %>

那说,你 能够 使用 form_for 对于非ActiveRecord对象,他们只需要响应某些方法。确保在Article模型中实现这些方法。

  • id
  • new_record?
  • to_param
  • errors

这只是猜测需要什么,可能还有其他人。如果实现了这些并且行为类似于ActiveRecord,那么您应该能够使用 form_for


16
2017-08-13 17:47



谢谢瑞恩。如果我决定在我的模型中添加对form_for的支持 - 有关如何确认所需的完整方法的任何建议吗? - Pauli Price
我不知道Rails 2中的一种方法来确定“完整”的必要方法集。如果你做了一些测试并且没有得到method_missing错误,那可能是好的。 Rails 3将引入一种记录的方式来执行此操作,并为您提供在模型中定义的已建立的界面。 - ryanb
好的 - 现在我太蹩脚了,无法弄清楚如何访问表单值!在我的控制器中我有:def create @article = Article.new(:title => params [:article])但是这不起作用。当我尝试在内部使用它来初始化类时,我得到以下内容:我按下“创建”#features / step_definitions / webrat_steps.rb:14未定义的方法`downcase'for {“title”=>“Spuds”}:HashWithIndifferentAccess (NoMethodError)任何线索? - Pauli Price
Article.new(params [:article])有用吗? - ryanb
'downcase'为{“title”=>“Spuds”}:HashWithIndifferentAccess基于该错误,我只能假设它将值“Spuds”存储在title参数中,类型为HashWithIndifferentAccess而不是String类型。因为它是couchdb模型,所以在分配之前它没有验证类型 - 我想我可以添加到模型中。但我现在对如何进行感到茫然。我以前从未听说过HashWithIndifferentAccess,现在只是阅读它。 - Pauli Price


答案:


form_tag 方法不使用表单生成器,因此您不能在表单块中使用“f”变量。代替 f.error_messages 你必须使用 error_messages_for

<% form_tag new_article_url(@article), :method => :post do %>
  <%= error_messages_for :article %>

  <p>
    <%= label :article, :title %><br />
    <%= text_field :article, :title %>
  </p>
  <p>
    <%= submit_tag 'Create' %>
  </p>
<% end %>

那说,你 能够 使用 form_for 对于非ActiveRecord对象,他们只需要响应某些方法。确保在Article模型中实现这些方法。

  • id
  • new_record?
  • to_param
  • errors

这只是猜测需要什么,可能还有其他人。如果实现了这些并且行为类似于ActiveRecord,那么您应该能够使用 form_for


16
2017-08-13 17:47



谢谢瑞恩。如果我决定在我的模型中添加对form_for的支持 - 有关如何确认所需的完整方法的任何建议吗? - Pauli Price
我不知道Rails 2中的一种方法来确定“完整”的必要方法集。如果你做了一些测试并且没有得到method_missing错误,那可能是好的。 Rails 3将引入一种记录的方式来执行此操作,并为您提供在模型中定义的已建立的界面。 - ryanb
好的 - 现在我太蹩脚了,无法弄清楚如何访问表单值!在我的控制器中我有:def create @article = Article.new(:title => params [:article])但是这不起作用。当我尝试在内部使用它来初始化类时,我得到以下内容:我按下“创建”#features / step_definitions / webrat_steps.rb:14未定义的方法`downcase'for {“title”=>“Spuds”}:HashWithIndifferentAccess (NoMethodError)任何线索? - Pauli Price
Article.new(params [:article])有用吗? - ryanb
'downcase'为{“title”=>“Spuds”}:HashWithIndifferentAccess基于该错误,我只能假设它将值“Spuds”存储在title参数中,类型为HashWithIndifferentAccess而不是String类型。因为它是couchdb模型,所以在分配之前它没有验证类型 - 我想我可以添加到模型中。但我现在对如何进行感到茫然。我以前从未听说过HashWithIndifferentAccess,现在只是阅读它。 - Pauli Price