为什么在使用 form_for 时出现 attribute_methods 错误?
当我尝试在部分视图或“显示”视图中执行此 form_for 时,我不断收到此错误。谁能解释我做错了什么?
显示 c:/.../show.html.erb 其中第 1 行提出:
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:272: syntax error, unexpected ')'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:273: syntax error, unexpected '?', expecting $end
Extracted source (around line #1):
1: <%= form_for(@post) do |f| %>
2: <div class="field">
3: <%= f.text_area :content %>
4: </div>
此时,我的控制器只有这个:
def show
@post = Post.new
end
我的视图只有:
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
我的模型是:
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# content :string(255)
# approved? :boolean
# kid_id :integer
# created_at :datetime
# updated_at :datetime
#
class Post < ActiveRecord::Base
belongs_to :kid
attr_accessible :content
validates :content, :presence => true, :length => { :maximum => 140 }
validates :kid_id, :presence => true
default_scope :order => 'posts.created_at DESC'
end
甚至尝试了两个不同版本的 Rails 3,但没有成功...
I keep getting this error when I attempt to do this form_for, either in a partial or in the "show" view. Can anyone explain what I'm doing wrong?
Showing c:/.../show.html.erb where line #1 raised:
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:272: syntax error, unexpected ')'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:273: syntax error, unexpected '?', expecting $end
Extracted source (around line #1):
1: <%= form_for(@post) do |f| %>
2: <div class="field">
3: <%= f.text_area :content %>
4: </div>
At this point, my controller only has this:
def show
@post = Post.new
end
And my view only has:
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
My model is:
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# content :string(255)
# approved? :boolean
# kid_id :integer
# created_at :datetime
# updated_at :datetime
#
class Post < ActiveRecord::Base
belongs_to :kid
attr_accessible :content
validates :content, :presence => true, :length => { :maximum => 140 }
validates :kid_id, :presence => true
default_scope :order => 'posts.created_at DESC'
end
Even tried two different versions of Rails 3 with no luck...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难确定,但根据您从视图中提供的代码和错误消息,您可能忘记用 end 关闭 form_for:
编辑:
查看您的模型,我实际上通过向其中添加一个问号来重现您的问题我的数据库列。
表中的列不应包含任何特殊字符,例如问号。重命名列已获批准?获得批准并且应该有效。
It's hard to tell for sure but based on the code you supplied from the view and the error message, you probably forgot to close the form_for with end:
Edit:
Looking at your model I actually managed to reproduce your problem by adding a questionmark to one of my database columns.
The columns in your tables should not contain any special characters like questionmark. Rename the column approved? to approved and it should work.