开发者

Ruby on Rails not saving data on post

开发者 https://www.devze.com 2023-04-05 11:33 出处:网络
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.

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开发者_JAVA技巧 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 :-)


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


$ 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.

0

精彩评论

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