为什么在使用 form_for 时出现 attribute_methods 错误?

发布于 2024-10-31 06:21:39 字数 1517 浏览 2 评论 0原文

当我尝试在部分视图或“显示”视图中执行此 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 技术交流群。

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

发布评论

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

评论(1

最后的乘客 2024-11-07 06:21:39

很难确定,但根据您从视图中提供的代码和错误消息,您可能忘记用 end 关闭 form_for:

<%= form_for(@post) do |f| %>
   <%= f.text_area :content %>
<% end %>

编辑:

查看您的模型,我实际上通过向其中添加一个问号来重现您的问题我的数据库列。

表中的列不应包含任何特殊字符,例如问号。重命名列已获批准?获得批准并且应该有效。

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:

<%= form_for(@post) do |f| %>
   <%= f.text_area :content %>
<% 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.

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