Rails form_tag 表单编写 - 带非活动记录模型
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
form_tag
方法不使用表单生成器,因此您无法在表单块中使用“f”变量。 您必须使用error_messages_for
等代替f.error_messages
。也就是说,您可以使用
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 off.error_messages
you have to useerror_messages_for
, etc.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
.