English 中文(简体)
铁路:如何在内部向另一个控制者采取行动?
原标题:Rails: How to POST internally to another controller action?

这样做会很奇怪,但听我说......。 我需要能够向我其他一名控制人员提出相等的《禁止杀伤人员地雷行动计划》要求。 <代码>SimpleController基本上是一个更精确的管制器的简化版本。 我如何能够适当地这样做?

class VerboseController < ApplicationController
  def create
    # lots of required params
  end
end

class SimpleController < ApplicationController
  def create
    # prepare the params required for VerboseController.create
    # now call the VerboseController.create with the new params
  end
end

也许我对这一点过于思考,但我不知道如何这样做。

问题回答

铁路客运通信(或根据同一模型-地点-勘察模式的任何网络信号)是你应积极避免的。 当你想这样做时,你就认为这是一个迹象,表明你正在反对你所期望的模式和框架,而且你在申请的错误层次上采用了逻辑。

正如“@ismaelga”在评论中建议的那样;两个控制者都应援引某些共同组成部分来处理这一共同行为,并保持你的控制者“亲属”。 在铁路中,通常使用一种模型物体的方法,特别是在制造行为类型方面,在这种情况下,你似乎担心。

你们没有这样做。 你们是否树立了榜样? 然后,在模型上采用两种类别方法会更好。 该法律还使法典更加分离。 然后,你不仅可以使用控制器的方法,而且今后还可以使用背景工作(特工)。

例如,如果你重新创造人:

class VerboseController < ApplicationController
  def create
    Person.verbose_create(params)
  end
end

class SimpleController < ApplicationController
  def create
    Person.simple_create(params)
  end
end

然后,在“个人”中,你可以这样做:

class Person
  def self.verbose_create(options)
    # ... do the creating stuff here
  end

  def self.simple_create(options)
    # Prepare the options as you were trying to do in the controller...
    prepared_options = options.merge(some: "option")
    # ... and pass them to the verbose_create method
    verbose_create(prepared_options)
  end
end

我希望这能够帮助一点。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签