0

我在使用 Rails 中的超级基本形式时遇到问题。我希望它为用户能够更改评论(正文)中的副本所做的一切,我不明白为什么这不起作用......

控制器:

def edit
@comment = Comment.find(params[:id])
end

def update
  @comment = Comment.find(params[:id])

respond_to do |format|
  if @comment.update_attributes(params[:comment])
    format.html { redirect_to :journal, notice: 'Comment Updated' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @comment.errors, status: :unprocessable_entity }
  end
end
end

看法:

<%= render :partial => "shared/edit_comment", :locals => { 
  :comment => @comment,
  :attribute => :body }  %>

表格部分:

<%= form_for(comment) do |f| %>

        <%= f.text_area attribute, :class => "mlm mtm", :style => "color:#aaa;", 'data-widearea' => 'enable' %>  

        <%= button_tag "Save Changes", :class => 'normal caps pam mlm mtm', :style => "width: 34%;" %>
      <% end %>

如果有帮助,请建模:

class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:update_id]

attr_accessible :update_id, :user_id, :body

validates_presence_of :body
validates_presence_of :user, :on => :create

# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_voteable

belongs_to :update

# NOTE: Comments belong to a user
belongs_to :user

after_create :create_notification

# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
  c = self.new
  c.update_id = obj.id
  c.body = comment
  c.user_id = user_id
  c
end

#helper method to check if a comment has children
def has_children?
  self.children.size > 0
end

# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
  where(:user_id => user.id).order('created_at DESC')
}

private 

 # after a comment is created we'll create notifications for the 
 # creator of the update, as well as for anyone else who has 
 # commented as part of this update.
 def create_notification
  if update.user.id != user.id
  Notification.comment_on_update(update.user, 
                                 user.username, 
                                 user.id, 
                                 update.id, 
                                 update.notification_type, 
                                 update.game.name)
end

# efficiently retrieves all the users who have commented on this 
# update without any duplicates. then ensures that we don't add a 
# notification for the author of the new comment, or the original
# creator of the update.
update.comments.includes(["user"]).collect { |c| c.user }.uniq do |commenter|
  if (commenter.id != update.user.id) and (commenter.id != user.id)
    Notification.comment_on_comment(commenter, 
                                    user.username, 
                                    user.id,
                                    update.user.username,
                                    update.user.id,
                                    update.id,
                                    update.notification_type,
                                    update.game.name)
  end
end
end

end

提交表单后开发日志的输出:

Started PUT "/comments/129" for 127.0.0.1 at 2013-11-01 19:19:16 -0700
Processing by CommentsController#update as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"bRre95bNaK0Y242yF+50TaOlBZEmuW4Lo9vDM68cOi0=", "comment"=>{"body"=>"WHY YOU NO WORK!!!!!??"}, "id"=>"129"}
  [1m[36mUser Load (0.5ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1[0m
  [1m[35mComment Load (0.3ms)[0m  SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1  [["id", "129"]]
  [1m[36m (0.1ms)[0m  [1mBEGIN[0m
  [1m[35mUpdate Load (64.3ms)[0m  SELECT "updates".* FROM "updates" WHERE "updates"."id" = 637 LIMIT 1
  [1m[36m (0.2ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/journal
Completed 302 Found in 104ms (ActiveRecord: 66.6ms)

无论日志如何,数据库都没有变化。任何想法都会很棒。

4

1 回答 1

0

我找到了答案。本质上,更新不是模型的合适名称。

您可以在下面的链接中找到更多详细信息,但将型号名称更改为其他名称可以解决问题。

Rails update_attribute 没有保存

https://rails.lighthouseapp.com/projects/8994/tickets/5772-calling-a-model-update-leads-to-unexpected-behavior

于 2013-11-02T22:42:07.813 回答