开发者

Ruby on Rails form_for post to database not working for one column but is for the others in same table

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?


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:

attr_accessible :content

I had to change it to:

attr_accessible :content, :heading
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜