2

这是我的第一个 Rails 项目。出于某种原因,我无法发布到数据库中的一列,尽管同一张表中的其他列运行良好。(当我填充同一个表中的其他列时,我也无法使用 Faker gem 填充该列,所以我只是进入数据库并手动填充它们,这样我就可以跳过它。)

我的数据库有一个表“articles”,其中包含“id”“content”“user_id”“created_at”“updated_at”和“heading”列。我创建新文章的表单是这样的:

<%= form_for @article do |f| %>
  <div class="field">
    <%= f.text_field :heading, :size => "60x1" %>
  </div>
  <div class="field">
    <%= f.text_area :content, :size => "60x24" %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

我的articles_controller 有:

def new
 @article = Article.new
end

def create
 if @article = current_user.articles.create!(params[:article])
   redirect_to root_path, :flash => { :success => "Article created!" }
 else
   redirect_to "/articles/new"
 end
end

当我尝试提交表单时,它要么捕获我的标题验证(:presence => true),要么如果我注释掉验证,则只提交没有标题。该帖子正在发送这些参数:

{"utf8"=>"✓",
"authenticity_token"=>"...",
"article"=>{"heading"=>"Test heading",
"content"=>"Test content"},
"commit"=>"Submit"}

好像有个标题。所有其他列都被填写,包括 user_id。为什么标题不填?我已经运行 rake db:migrate 并将 reset_column_information 添加到我的迁移中:

def self.up
 add_column :articles, :heading, :string
 Article.reset_column_information
end

但这两种方式都不起作用。

此外,如果我进入 rails 控制台并以这种方式添加新文章,我可以毫无问题地添加标题。

如果重要的话,我正在使用 SQLite。我在以后的迁移中添加了标题列,它在列顺序中排在最后会是一个问题吗?

4

1 回答 1

2

我已经讨论了好几天了,实际上我一发布它就解决了它。在我的 article.rb 中,我有:

attr_accessible :content

我不得不将其更改为:

attr_accessible :content, :heading
于 2011-04-01T00:15:03.570 回答