gilbert
gilbert

Reputation: 318

Ruby on Rails not saving data on post

I have CRUD that when creating a new entry, nothing is saved and no error is generated. I can see the post happening in the logs.

Controller

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(params[:article])
    if @article.save
      redirect_to @article
    else
      render :action => 'new'
    end
  end

Model

class Article < ActiveRecord::Base
    attr_accessible :title, :content end

Routes

map.resources :articles

Log

Parameters: {"article"=>{"title"=>"Dive trip", "content"=>"hfigoo"}, "commit"=>"Save changes"}

Any help appreciated :-)

Upvotes: 1

Views: 3927

Answers (2)

sameera207
sameera207

Reputation: 16629

One problem might be that it's failing its validations. But yes, as per the code I can't see any. You can do something like this

First check if the request comes to the create method, putting a simple puts command will do this.

If the request comes to the method use @article.save! instead of @article.save, this will tell you if there are any validation errors

Upvotes: 3

Travis
Travis

Reputation: 10547

$ script/console
> a = Article.new({"title"=>"Dive trip", "content"=>"hfigoo"})
  => #<Article:.....
> a.save

When you see the response from a.save as either => false or => true. If SQL is executed, you should be able to see it in the console.

If you must run it on heroku, you can do

$ heroku console

to get the rails console on the heroku platform.

Upvotes: 1

Related Questions