开发者

Create and Update actions, is this good style?

I am updating a user object, but before the save/update I need to perform some parsing on a property of the object, do I have to repeat it or should I be using a filter to do this before the save?

example:

def create
   @user = User.new(params[:user]

   @user.parsed_bio = parse_bio(@user.bio)

   if @user.save
      ...
   end
end

def update
   @user = User.find(par开发者_运维知识库ams[:id])

   if @user.update_attributes(params[:user])
   ...
   end

end

The parsed_bio property isn't updated via the params, I have to do it explicilty.

Should I just be repeating the assignment in both the create and update, or can I do this in a single place using a filter somehow?


You could do it in the User's model:

class User < ActiveRecord::Base
  before_save :parse_bio

  private
  def parse_bio
    # parse your self.bio here
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜