0

I'm trying to determine how to keep my controller DRY, and I have what might be considered a sub view (or control in other platforms) that will be included in many places in the app.

Is there a typical RoR DRY approach to handling this?

An example, if it helps, might be a "rate this page" type control/view. It will be included in different types of page controllers, and it doesn't strike me as an "extension" of the given page controller. Another suitable example may be a "contact us" form that would be available without a "dedicated" view.

Here is another similar question without an answer: https://stackoverflow.com/questions/18408016/initializing-object-for-modal-form

The only comment admits that their proposed solution breaks standard design practices.

Further details:

An element of a view (some might consider it a control) that requires access to session data (to load user specific data, or to ensure that the user is only allowed to action once per session, etc.), access to validation information (for error handling on post), and may be included in may different views.

The issue that I'm currently trying to "fix" is that of "DRY"ing up the code in the cases that the element is included in views that already have view specific logic and output.

So let's consider the contact form element as an example. I want the ability for the client to submit a message at the bottom of most pages (different controllers make up the site obviously). One of the controllers happens to be a news page with some database output (articles). So this one controller is already designed to look after news articles and its views. Why would I put an initialiser in that controller for contact form? If the user tries to post an invalid form, who gets the error? I think the error should be inline, so now the news articles have to be re-populated as well as the contact form needs to appropriately alert the user.

class PageController < ApplicationController
  before_filter :collect_for_output


  def collect_for_output
    # some kind of business logic for grabbing the correct articles for
    # page we're on
    @articles= Article.all
  end

  def index
    # this initialisation doesn't belong in this controller
    # it should be in its own controller
    # I'm talking about initialisation that is dependent on session data
    # Say for a supplied name field I might pre-populate it with the
    # current users's name, otherwise allow it to be sent anonymously
    @message = Message.new
  end

  def post_message
    # this does *not* belong here but how else do I handle validation
    # in the current view?!? also how do I determine which view in this
    # controller made the post
  end
end


class NotAPageController < ApplicationController
  # this controller is nothing like a PageController, but I might
  # want the contact view available in these views too... how do I handle
  # the initialisation? (in these cases I'm talking about common
  # initialisation)
end

I am considering that my approach is all wrong, would be happy to be educated. I'd like to reiterate "rendering a partial with model that is only default initialised" is not the answer.

4

1 回答 1

2

如果我理解您的问题,您需要的是部分.

按照您的示例,您可以创建一个_contact_form.html.erb在视图/共享中调用的部分模板。因此,您可以在应用程序的多个视图中添加“联系我们”表单。为了保持 DRY,创建一个contact_controller.rb带有 create 操作的新控制器来处理表单中发送的信息。

希望这可以帮助。

于 2013-09-07T02:33:11.413 回答