【问题标题】:Nested (and Unested at-same-time) Resources better approach嵌套(和同时未嵌套)资源更好的方法
【发布时间】:2013-01-09 11:00:24
【问题描述】:

我有两个这样的模型

class Plan < ActiveRecord::Base
    belongs_to :profile

和

class Profile < ActiveRecord::Base
    has_many :plans

还有类似的路线:(我需要)

resources :profiles do
    resources :plans
end
resources :plans

所以,跟进ruby-on-rails - Problem with Nested Resources,我已经像这样制作了我的 PLANS 索引控制器,以同时工作 NESTED 和 UNESTED(我唯一的方法是已经找到了):

def index
  if params.has_key? :profile_id
    @profile = Profile.find(params[:profile_id])
    @plans = @profile.plans
  else
    @plans = Plan.all
end

有更简洁的方法吗?

在这种情况下,我有另一个模型,并且将所有操作、所有控制器中的行为都像这样处理是很麻烦的。

【问题讨论】:

    标签: ruby-on-rails ruby nested dry


    【解决方案1】:

    你给了我一个想法:

    models/user.rb:

    class User < ActiveRecord::Base
      has_many :posts
      attr_accessible :name
    end
    

    模型/post.rb:

    class Post < ActiveRecord::Base
      belongs_to :user
      attr_accessible :title, :user_id
    end
    

    控制器/posts_controller.rb:

    class PostsController < ApplicationController
      belongs_to :user # creates belongs_to_user filter
    
      # @posts = Post.all # managed by belongs_to_user filter
    
      # GET /posts
      # GET /posts.json
      def index
        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @posts }
        end
      end
    end
    

    现在是实质:

    控制器/application_controller.rb:

    class ApplicationController < ActionController::Base
      protect_from_forgery
    
      def self.belongs_to(model)
                                                          # Example: model == :user
        filter_method_name = :"belongs_to_#{model}_index" # :belongs_to_user_index
        foreign_key        = "#{model}_id"                # 'user_id'
        model_class        = model.to_s.classify          # User
    
        class_eval <<-EOV, __FILE__, __LINE__ + 1
          def #{filter_method_name}                                                # def belongs_to_user_index
            if params.has_key? :'#{foreign_key}'                                   #   if params.has_key? :user_id
              instance_variable_set :"@#{model}",                                  #     instance_variable_set :"@user",
                                    #{model_class}.find(params[:'#{foreign_key}']) #                           User.find(params[:user_id])
              instance_variable_set :"@\#{controller_name}",                       #     instance_variable_set :"@#{controller_name}",
                                    @#{model}.send(controller_name.pluralize)      #                           @user.send(controller_name.pluralize)
            else                                                                   #   else
              instance_variable_set :"@\#{controller_name}",                       #     instance_variable_set :"@#{controller_name}",
                                    controller_name.classify.constantize.all       #                           controller_name.classify.constantize.all
            end                                                                    #   end
          end                                                                      # end
        EOV
    
        before_filter filter_method_name, only: :index # before_filter :belongs_to_user_index, only: :index
      end
    end
    

    如果您有 Ruby 元编程的概念,该代码并不复杂理解:它声明了一个 before_filter,它声明了实例变量,从控制器名称和关联中推断名称。它仅针对索引操作实现,这是唯一使用复数实例变量版本的操作,但为其他操作编写过滤器版本应该很容易。

    【讨论】:

    • 似乎是一个不错的技巧,即使对于像我这样的新手也是如此。之后我会测试并接受。谢谢:)
    • 我发现第一个替代方案更清晰。这可能会让同事感到困惑。
    • 如果你的同事看到 Rails 源代码,他们会害怕地跑掉 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2016-05-02
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多