Rails form_tag表单编写-具有非活动记录模型 [英] Rails form_tag form writing - with non-active record model

查看:105
本文介绍了Rails form_tag表单编写-具有非活动记录模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点像Rails的新手.我正在编写一个沙发靠背应用程序,因此未在该模型上使用activerecord.我刚刚发现这意味着

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) 

不起作用.我正在尝试找出如何使用form_tag的方法-但大多数示例都不涉及新的&创建动作.

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.

这是错误的:

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

推荐答案

form_tag方法不使用表单生成器,因此不能在表单块中使用"f"变量.代替f.error_messages,您必须使用error_messages_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 %>

也就是说,您可以对非ActiveRecord对象使用form_for,它们只需要响应某些方法即可.确保在Article模型中实现了这些方法.

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
  • id
  • new_record?
  • to_param
  • errors

这只是对所需内容的猜测,可能还有其他内容.如果实现了这些功能并且行为类似于ActiveRecord,那么您应该能够使用form_for.

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.

这篇关于Rails form_tag表单编写-具有非活动记录模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆