Ruby on Rails form_for 发布到数据库不适用于某一列,但适用于同一表中的其他列
这是我的第一个 Rails 项目。由于某种原因,我无法发布到数据库中的一列,尽管同一个表中的其他列运行良好。 (当我填充同一个表中的其他列时,我也无法使用 Faker gem 填充该列,所以我只是进入数据库并手动填写它们,这样我就可以跳过它。)
我的数据库有一个表“文章”,其中包含“id”“内容”“user_id”“created_at”“updated_at”和“标题”。我创建新文章的表单如下:
<%= 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。我在以后的迁移中添加了标题列,它在列顺序中排在最后可能是一个问题吗?
This is my first Rails project. For some reason, I can't post to one column in my database, although the other columns in the same table work out fine. (I also couldn't populate that column using the Faker gem when I populated the other columns in the same table, so I just went into the database and filled them in by hand just so I could move past it.)
My database has a table "articles" with columns "id" "content" "user_id" "created_at" "updated_at" and "heading". My form to create a new article is like this:
<%= 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 %>
My articles_controller has:
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
When I try to submit the form, it either catches on my heading validation (:presence => true) or just submits without a heading if I comment out the validation. The post is sending these parameters:
{"utf8"=>"✓",
"authenticity_token"=>"...",
"article"=>{"heading"=>"Test heading",
"content"=>"Test content"},
"commit"=>"Submit"}
It looks like there's a heading. All of the other columns get filled in, including user_id. Why doesn't the heading get filled in? I have run rake db:migrate and also added reset_column_information to my migration:
def self.up
add_column :articles, :heading, :string
Article.reset_column_information
end
But it didn't work either way.
Also, if I go into the rails console and add a new article that way, I can add a heading with no problem.
If it matters, I am using SQLite. I added the heading column in a later migration, could it be a problem that it comes last in the column order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经研究这个问题好几天了,实际上我一发布就解决了它。在我的文章.rb 中,我有:
我必须将其更改为:
I have been going over this for days and I actually just solved it as soon as I posted it. In my article.rb, I had:
I had to change it to: