【问题标题】:Refactoring: How to efficiently render json in Rails update action重构:如何在 Rails 更新操作中有效地呈现 json
【发布时间】:2011-04-29 00:14:16
【问题描述】:

我如何重构这段代码,这样当它们使用相同的基本格式时,我就不会一遍又一遍地重复 json 对象?我对 Ruby on Rails 还是有点不适应,所以我不确定解决这个问题的最佳方法。

  # 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

【问题讨论】:

    标签: ruby-on-rails json ruby-on-rails-3 refactoring dry


    【解决方案1】:

    将那个巨大散列中的数据设为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
    

    然后

    # 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
    

    【讨论】:

    • 我不得不稍微修改你的代码才能让它工作,因为它一直说“prop不是有效的方法”。我将修改后的部分发布到我原来的问题中。感谢您让我走上正轨!!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2013-09-08
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多