嵌套(和同时出现)资源更好的方法

时间:2013-01-25 13:12:26

标签: ruby-on-rails ruby nested dry

我有两个这样的模型

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

有一种更清洁的方法吗?

在这种情况下我有另一个模型,并且在所有控制器中执行所有操作都表现得很麻烦。

1 个答案:

答案 0 :(得分:0)

你给了我一个主意:

模型/ 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,它声明了实例变量,从控制器名称和关联中推断出名称。它仅针对索引操作实现,这是使用复数实例变量版本的唯一操作,但是应该很容易为其他操作编写过滤器版本。