控制器#更新的奇怪事件根本不更新模型

时间:2012-07-18 10:40:33

标签: ruby-on-rails ruby

我有一个奇怪的案例。我的同事和我都不能弄明白为什么它不起作用。也许有人有类似的情况或者可以发现我们正在忽视的任何东西。这就是我们所拥有的:

Rails 3.2.6 mysql数据库

我们有一个Comment模型。像这样:

# == Schema Information
#
# Table name: comments
#
#  id           :integer          not null, primary key
#  username     :string(255)
#  realname     :string(255)
#  email        :string(255)
#  title        :string(255)
#  content      :text
#  comment_date :datetime
#  article_id   :string(255)
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  rating       :float
#

class Comment < ActiveRecord::Base
  attr_accessible :username, :realname, :email, :title, :content, :comment_date, :article_id, :rating
  belongs_to :article, class_name: Goldencobra::Article, foreign_key: "article_id"
end

我用attr_accessible试了一下而没有效果 我像这样创建了CommentsController

class CommentsController < ApplicationController
  skip_before_filter  :verify_authenticity_token
  protect_from_forgery only: [:index]

  def index
    @article = Goldencobra::Article.find(params[:id])
    @comments = @article.comments
    respond_to do |format|
      format.rss
    end
  end

  def create
    @comment = Comment.new(params[:comment])
    @comment.save!
    flash[:notice] = 'Comment was successfully created.'
  rescue ActiveRecord::RecordInvalid
    render :action => :new
  end

  def update
    @comment = Comment.find(params[:id])
    if @comment && @comment.update_attributes(params[:comment])
      render text: "Success", layout: false
    else
      render text: "No success", layout: false
    end
  end
end

我想要的是使用更新操作来更新评论模型。就像我们每次都做的一样。小菜一碟… 只是,它没有更新。以下是我测试更新的方法:

require 'spec_helper'

describe CommentsController do

  def do_create
    @article = Goldencobra::Article.create(title: "Testartikel", article_type: "Default Show")
    post :create, :comment => {username: "johnny",
                               realname: "John Doe",
                               email: "johnny@gmail.com",
                               title: "Great title",
                               content: "What an awesome article.",
                               article_id: @article.id,
                               rating: 3.4}
  end

  it "should update a comment" do
    do_create
    put :update, id: Comment.first.id, comment: {title: "New great title"}
    Comment.first.title.should eql("New great title")
  end
end

我尝试通过REST客户端进行更新(OS X上的Rested.app) Restful try params Restful try response

我总是得到“成功”回应。 .update_attributes()总是成功的。事情是:它什么都没改变。这是来自restful try的控制台日志:

Started PUT "/comments/10" for 127.0.0.1 at 2012-07-18 12:35:13 +0200
Processing by CommentsController#update as HTML
  Parameters: {"comment"=>{"title"=>"neuer test"}, "id"=>"10"}
  Comment Load (0.4ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`id` = 10 LIMIT 1
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
  Rendered text template (0.0ms)
Completed 200 OK in 4ms (Views: 0.4ms | ActiveRecord: 0.6ms | Solr: 0.0ms)

当我在rails控制台中尝试相同的瘦身时,这就是它的样子:

1.9.2p290 :017 > Comment.last.update_attributes(title: "test 2")
  Comment Load (0.8ms)  SELECT `comments`.* FROM `comments` ORDER BY `comments`.`id` DESC LIMIT 1
   (0.2ms)  BEGIN
   (0.5ms)  UPDATE `comments` SET `title` = 'test 2', `updated_at` = '2012-07-18 09:48:39' WHERE `comments`.`id` = 10
   (3.4ms)  COMMIT
 => true 

缺少的是UPDATE条评论SET标题= 'test 2', updated_at = '2012-07-18 09:48:39' WHERE评论. id = 10。谁能告诉我为什么我的控制器不更新模型?

谢谢。

2 个答案:

答案 0 :(得分:0)

你确定你没有尝试将'title'更新为旧值吗?我知道这听起来很愚蠢,但有时我会忽略一些显而易见的事情,需要另一个人给我指出一些明显的事情。

答案 1 :(得分:0)

您的其他客户端在您的参数散列中缺少一个参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"gzC0IKScOUu+RBacL1YREpe4aE4KgnD0lqOgMhwRKVA="

它是authenticity_token。它位于Rails应用程序表单中:

<input name="authenticity_token" type="hidden" value="gzC0IKScOUu+RBacL1YREpe4aE4KgnD0lqOgMhwRKVA=" />

因此您无法从其他应用程序更新记录(正如您尝试的那样)。您可以在过滤之前跳过此步骤:

skip_before_filter :verify_authenticity_token

在您的控制器中。