2

我有一个奇怪的案例。我的同事和我都无法弄清楚为什么它不起作用。也许有人有类似的情况,或者可以发现我们忽略的任何东西。这是我们所拥有的:

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)更新尝试了同样的事情 宁静的尝试参数 宁静的尝试响应

我总是得到“成功”的回应。.update_attributes()总是成功。问题是:它没有任何改变。这是来自 restful 尝试的控制台日志:

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。谁能告诉我为什么我的控制器不更新模型?

谢谢你。

4

2 回答 2

0

您的其余客户端在您的 params 哈希中缺少一个参数:

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

它是authenticity_token。它在 Rails 应用程序表单中的位置为:

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

因此,您无法从其他应用程序更新记录(正如您尝试做的那样)。您可以在使用以下过滤器之前跳过此操作:

skip_before_filter :verify_authenticity_token

在您的控制器中。

于 2012-07-18T16:53:30.113 回答
0

您确定您没有尝试将“标题”更新为其旧值吗?我知道这听起来很愚蠢,但有时我忽略了一些明显的事情,需要另一个人来指出我一些明显的事情。

于 2012-07-18T12:29:54.227 回答