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沒有變化。任何想法都會很棒。
什麼是 「形式」? ..並把完整的日誌後參數。 –
「不工作」是什麼意思?是@ comment.update_attributes(params [:comment])'返回'false'嗎? – pdoherty926
@Gopalrathore @Gopalrathore對不起,不知何故'表格'被切斷也增加了更多來自日誌的信息 –