Rails render of partial and layout in controller

后端 未结 3 370
星月不相逢
星月不相逢 2021-01-04 10:04

I am overriding the create action of the devise Registrations Controller. I have two forms for signup, individual or company, a company has a field called company_form set t

相关标签:
3条回答
  • 2021-01-04 10:42

    Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.

    This may be the reason your code is not working you can use rendering template.

    Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.

    if resource.company_form
       render :template => "shared/company_signup_form"
    else
       render :template => "shared/individual_signup_form"   
    end
    

    ** REMOVE UNDERSCORE from your partail name because you are using this as a template.

    Hope this works !

    0 讨论(0)
  • 2021-01-04 10:55

    In the end (and i know this is a hack) but i created a partial called _partial_layout_wrapper in my layouts that was an exact copy of the layouts/application file and used this in my controller

    render partial: 'shared/company_signup_form', layout: 'partial_layout_wrapper'
    

    this works but surely this cannot be the way ?

    0 讨论(0)
  • 2021-01-04 10:58

    Could you post your whole controller? Rails renders by default layout/application.html.erb if another layout is not specified.

    From the ror guides:

    Partial Layouts

    A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:

    <%= render partial: "link_area", layout: "graybar" %>
    

    This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master layouts folder).

    Also note that explicitly specifying :partial is required when passing additional options such as :layout.

    So render partial: 'shared/company_signup_form', layout: 'layouts/application'is looking for layouts/_application.html.erb

    0 讨论(0)
提交回复
热议问题