Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange incident of Controller#update not updating model at all
    primarykey
    data
    text
    <p>I have a strange case. Neither my co-worker nor me can figure out why it doesn't work. Perhaps someone had a similar case or can spot whatever it is we are overlooking. Here's what we have:</p> <p>Rails 3.2.6 mysql Database</p> <p>We have a <code>Comment</code> model. Like this:</p> <pre><code># == 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 &lt; 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 </code></pre> <p>I tried it with <code>attr_accessible</code> and without to no effect.<br> I created a <code>CommentsController</code> like this:</p> <pre><code>class CommentsController &lt; 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 =&gt; :new end def update @comment = Comment.find(params[:id]) if @comment &amp;&amp; @comment.update_attributes(params[:comment]) render text: "Success", layout: false else render text: "No success", layout: false end end end </code></pre> <p>What I want to to is use the update action to update the comment model. Like what we do every single time. Piece of cake… Only, it doesn't update. Here is how I test for the update:</p> <pre><code>require 'spec_helper' describe CommentsController do def do_create @article = Goldencobra::Article.create(title: "Testartikel", article_type: "Default Show") post :create, :comment =&gt; {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 </code></pre> <p>I tried the same thing with updating via a REST-client (Rested.app on OS X) <img src="https://i.stack.imgur.com/xpmbK.png" alt="Restful try params"> <img src="https://i.stack.imgur.com/uGx7F.png" alt="Restful try response"></p> <p>I always get a "Success" response. <code>.update_attributes()</code> always succeeds. Thing is: It changes nothing. Here's the console log from the restful try:</p> <pre><code>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"=&gt;{"title"=&gt;"neuer test"}, "id"=&gt;"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) </code></pre> <p>This is what it looks like, when I try the same thin in the rails console:</p> <pre><code>1.9.2p290 :017 &gt; 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 =&gt; true </code></pre> <p>What's missing is <code>UPDATE</code>comments<code>SET</code>title<code>= 'test 2',</code>updated_at<code>= '2012-07-18 09:48:39' WHERE</code>comments<code>.</code>id<code>= 10</code>. Can anybody tell me why my controller does not update the model?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload