Ruby on rails 重构:如何在Rails更新操作中高效地呈现json

Ruby on rails 重构:如何在Rails更新操作中高效地呈现json,ruby-on-rails,json,ruby-on-rails-3,refactoring,dry,Ruby On Rails,Json,Ruby On Rails 3,Refactoring,Dry,当json对象使用相同的基本格式时,如何重构此代码以避免重复json对象?我仍然对RubyonRails有点不舒服,所以我不确定最好的方法 # PUT /users/:user_id/profile/:id def update if request.xhr? profile = current_user.profile if params[:profile].blank? render :json => { :error =>

当json对象使用相同的基本格式时,如何重构此代码以避免重复json对象?我仍然对RubyonRails有点不舒服,所以我不确定最好的方法

  # PUT /users/:user_id/profile/:id
  def update
    if request.xhr?
      profile = current_user.profile

      if params[:profile].blank?
        render :json => { :error => "There was no profile data passed in so your profile could not be saved." }, 
               :status => :unprocessable_entity
      else
        if profile.update_attributes(params[:profile])
          render :json => { :interests => 
                              simple_format(h(profile.interests), :class => "pbs tl"),
                            :favorite_music =>
                              simple_format(h(profile.favorite_music), :class => "pbs tl"),
                            :favorite_movies =>
                              simple_format(h(profile.favorite_movies), :class => "pbs tl"),
                            :favorite_books =>
                              simple_format(h(profile.favorite_books), :class => "pbs tl"),
                            :favorite_foods =>
                              simple_format(h(profile.favorite_foods), :class => "pbs tl") },
                 :status => :ok
        else
          render :json => { :error => get_errors_for_class(profile).to_sentence }, 
                 :status => :unprocessable_entity
        end
      end
    end
  end

更新:我对原始答案做了一点修改,它对我有用。这是我的修改:

  # within profile.rb
  # create a hash of profile attributes
  def html_to_hash
    %w{interests favorite_music favorite_books favorite_foods}.inject({}) do |hash, property|
      hash[property] = simple_format(h(self.send(property)), :class => 'pbs tl')
      hash
    end    
  end

将该巨大散列中的数据作为
Profile
的方法

class Profile
  def to_hash
    [:interests, :favorite_music, :favorite_books, :favorite_foods].inject({}) do |h, prop|
      h[prop] = simple_format(h(self.send(:prop)), :class => 'pbs tl')
      h
    end
  end
end
然后


您可以在Profile类上创建一个
as_json
方法,该方法返回相同的散列,然后只需执行
render:json=>Profile
。另一种方法是插入并告诉它要序列化哪些属性以及如何序列化。

我不得不稍微修改一下代码,使其正常工作,因为它一直说“prop不是有效的方法”。我把修改过的部分贴到了我原来的问题上。谢谢你让我走上正轨!!
# PUT /users/:user_id/profile/:id
  def update
    if request.xhr?
      profile = current_user.profile

      if params[:profile].blank?
        render :json => { :error => "There was no profile data passed in so your profile could not be saved." }, 
               :status => :unprocessable_entity
      else
        if profile.update_attributes(params[:profile])
          render :json => profile.to_hash,
                 :status => :ok
        else
          render :json => { :error => get_errors_for_class(profile).to_sentence }, 
                 :status => :unprocessable_entity
        end
      end
    end
  end