奇妙なケースがあります。同僚も私も、なぜそれが機能しないのか理解できません。おそらく誰かが同様のケースを持っていたか、私たちが見落としているものを見つけることができます。これが私たちが持っているものです:
Rails3.2.6mysqlデータベース
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()
常に成功します。物事は:それは何も変更しません。安らかな試行からのコンソールログは次のとおりです。
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)
レールコンソールで同じシンを試してみると、次のようになります。
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
です。コントローラーがモデルを更新しない理由を誰かに教えてもらえますか?
ありがとうございました。