Ruby on rails 在另一个类中调用方法?
我在不同的命名空间(a和b)中有两个控制器,如下所示:Ruby on rails 在另一个类中调用方法?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在不同的命名空间(a和b)中有两个控制器,如下所示: class A::TechnologiesController < ApplicationController def index render json: Technology.all end end class B::TechnologiesController < ApplicationController def index render json: Technology.all end
class A::TechnologiesController < ApplicationController
def index
render json: Technology.all
end
end
class B::TechnologiesController < ApplicationController
def index
render json: Technology.all
end
end
class A::TechnologiesController
这两个动作执行相同的逻辑,我相信这是一个重复。我想消除重复,那么如何借用命名空间a中的代码呢
class B::TechnologiesController < ApplicationController
def index
A::TechnologiesController.method(:index).call self
end
end
class B::TechnologiesController
这是一个很好的例子。有了它,您就可以干涸代码,而不必调用另一个控制器的方法 以下是模块:
module CommonInterface
def render_technology
render :json, Technology.all
end
end
这就是你的控制器
class B::TechnologiesController < ApplicationController
include CommonInterface
def index
render_technology
end
end
class ApplicationController < ActionController::Base
def index
@collection = model
end
end
class B::TechnologiesController
这是一个很好的例子。有了它,您就可以干涸代码,而不必调用另一个控制器的方法 以下是模块:
module CommonInterface
def render_technology
render :json, Technology.all
end
end
这就是你的控制器
class B::TechnologiesController < ApplicationController
include CommonInterface
def index
render_technology
end
end
class ApplicationController < ActionController::Base
def index
@collection = model
end
end
class B::TechnologiesController
回答注释中的一个隐式问题:如果且只有调用bind
的对象是该方法所属类的实例,则可以借用该方法:
def index
A::TechnologiesController.instance_method(:index).bind(self).()
end
但这既不惯用,也不可读。应使用mixin:
module Mixins::TechnologiesController
def index
render json: Technology.all
end
end
class A::TechnologiesController < ApplicationController
include Mixins::TechnologiesController
end
class B::TechnologiesController < ApplicationController
include Mixins::TechnologiesController
end
回答注释中的一个隐含问题:如果且仅调用bind
的对象是该方法所属类的实例,则可以借用该方法:
def index
A::TechnologiesController.instance_method(:index).bind(self).()
end
但这既不惯用,也不可读。应使用mixin:
module Mixins::TechnologiesController
def index
render json: Technology.all
end
end
class A::TechnologiesController < ApplicationController
include Mixins::TechnologiesController
end
class B::TechnologiesController < ApplicationController
include Mixins::TechnologiesController
end
如果你真的想让事情干涸,祖先和mixin的答案真的很好,但在我看来,你不一定非得这么做。呈现json:
位属于控制器操作。您希望您的操作在两个控制器中都呈现某些内容
随着时间的推移,查询可能会以同样的方式发生变化,我猜是技术
如果您使用mixin或基类,您将耦合这两个控制器,这可能很好。不过,你可能只是想稍后再做决定。如果你真的想把事情干掉,祖先和混合的答案真的很好,但在我看来,你不一定非得这么做。呈现json:
位属于控制器操作。您希望您的操作在两个控制器中都呈现某些内容
随着时间的推移,查询可能会以同样的方式发生变化,我猜是技术
如果您使用mixin或基类,您将耦合这两个控制器,这可能很好。不过,您可能只是想稍后再做决定。您可以通过在ApplicationController中创建一个超级方法来解决上述问题
您需要在每个控制器中添加一个以上的方法,以将值传递给超级方法您不仅可以用于技术模型,还可以用于任何其他模型
例如,示例
在应用程序控制器中
class B::TechnologiesController < ApplicationController
include CommonInterface
def index
render_technology
end
end
class ApplicationController < ActionController::Base
def index
@collection = model
end
end
class ApplicationController
在每个控制器中,我们可以这样调用上述方法
class StudentsController < ApplicationController
def index
super
end
def model
Student
end
end
class JobsController < ApplicationController
def index
super
end
def model
Job
end
end
class StudentsController
例如,您将在@collection中接收到的数据可以在视图中使用
对于app/views/students/index.html.erb
<% @collection.all.each do |student| %>
<tr>
<td><%= student.Name %></td>
<td><%= student.Email %></td>
</tr>
<% end %>
<% @collection.all.each do |job| %>
<tr>
<td><%= job.id %></td>
<td><%= job.exp %></td>
</tr>
<% end %>
对于app/views/jobs/index.html.erb
<% @collection.all.each do |student| %>
<tr>
<td><%= student.Name %></td>
<td><%= student.Email %></td>
</tr>
<% end %>
<% @collection.all.each do |job| %>
<tr>
<td><%= job.id %></td>
<td><%= job.exp %></td>
</tr>
<% end %>
您可以通过在ApplicationController中创建超级方法来解决上述问题
您需要在每个控制器中添加一个以上的方法,以将值传递给超级方法您不仅可以用于技术模型,还可以用于任何其他模型
例如,示例
在应用程序控制器中
class B::TechnologiesController < ApplicationController
include CommonInterface
def index
render_technology
end
end
class ApplicationController < ActionController::Base
def index
@collection = model
end
end
class ApplicationController
在每个控制器中,我们可以这样调用上述方法
class StudentsController < ApplicationController
def index
super
end
def model
Student
end
end
class JobsController < ApplicationController
def index
super
end
def model
Job
end
end
class StudentsController
例如,您将在@collection中接收到的数据可以在视图中使用
对于app/views/students/index.html.erb
<% @collection.all.each do |student| %>
<tr>
<td><%= student.Name %></td>
<td><%= student.Email %></td>
</tr>
<% end %>
<% @collection.all.each do |job| %>
<tr>
<td><%= job.id %></td>
<td><%= job.exp %></td>
</tr>
<% end %>
对于app/views/jobs/index.html.erb
<% @collection.all.each do |student| %>
<tr>
<td><%= student.Name %></td>
<td><%= student.Email %></td>
</tr>
<% end %>
<% @collection.all.each do |job| %>
<tr>
<td><%= job.id %></td>
<td><%= job.exp %></td>
</tr>
<% end %>
要么让两个控制器都从一个共同的祖先继承(class a::TechnologiesController
),要么包含一个提供方法的模块。@Stefan所以没有一个方法可以在另一个类(如javascript)中借用该方法?我个人认为,在不同的控制器中,您不需要进行任何混合。更具可读性。您可以在此处使用ActiveModel::Concern。您可以定义一个相关的方法,并将其包含在控制器中。@运行什么命令