2013-11-02 64 views
0

我在rails中遇到超级基本表单的问题。所有我想要它做的用户能够改变评论(体)的副本,我不明白为什么这是不工作...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) 

无论日志的,有到DB没有变化。任何想法都会很棒。

+0

什么是 “形式”? ..并把完整的日志后参数。 –

+0

“不工作”是什么意思?是@ comment.update_attributes(params [:comment])'返回'false'吗? – pdoherty926

+0

@Gopalrathore @Gopalrathore对不起,不知何故'表格'被切断也增加了更多来自日志的信息 –

回答

相关问题