Rails form_tag 表单编写 - 带非活动记录模型

发布于 2024-08-02 00:17:50 字数 1546 浏览 8 评论 0原文

我是 Rails 的新手。 我正在编写一个 couchrest-rails 应用程序,因此我没有为此模型使用 activerecord。 我刚刚发现这意味着这是

form_for(@model) 

行不通的。 我正在尝试弄清楚如何使用 form_tag ——但大多数示例不涉及 new & 创建动作。

这是错误的:

<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"'

但我不明白这个错误,也不明白如何修复它。

I'm somewhat of a Rails newbie. I'm writing a couchrest-rails app, so am not using activerecord for this model. I just figured out that that means that

form_for(@model) 

won't work. I'm trying to work out how to use form_tag -- but most of the examples don't involve new & create actions.

This is wrong:

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

Because when I run my Cucumber scenario, I get this:

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"'

But I don't understand the error, or how to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2024-08-09 00:17:50

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

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

The form_tag method does not use a form builder, so you can't use the "f" variable in the form block. Instead of f.error_messages you have to use error_messages_for, etc.

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

That said, you can use form_for with non ActiveRecord objects, they just need to respond to certain methods. Ensure that these methods are implemented in the Article model.

  • id
  • new_record?
  • to_param
  • errors

That's just a guess as to what is needed, there may be others. If these are implemented and behave like ActiveRecord does you should be able to use form_for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文