Rails rendering partial and layout in the controller

I override the create action for the registration controller. I have two forms for registration, individual or company, the company has a field called company_form set to true, which distinguishes the two forms.

After checking the form, I need the correct form for rendering (previously it returned to the default form no matter what form I used).

I had a problem when only a partial image is displayed (obviously, since I only show a partial), but I need a layout / application file that will be displayed as well.

class RegistrationsController < Devise::RegistrationsController def create <!-- Other devise code here --> if resource.company_form render partial: 'shared/company_signup_form' else render partial: '/shared/individual_signup_form' end end end 

I tried

 if resource.company_form render partial: 'shared/company_signup_form', layout: 'layouts/application' else render partial: '/shared/individual_signup_form', layout: 'layouts/application end 

But I get an error

 Template is missing Missing partial layouts/_application 

Why is he looking for a partial application, when I specified the layout and how can I get the correct layout to be applied, please

thanks

Edit

After reading the documentation, he says

"Please note that layouts for partitions follow the same name of the leading-underline as regular partial ones, and are placed in the same partial folder to which they belong (not in the master layouts folder).

But I want to use the default layout

+6
source share
3 answers

Partial rendering in the controller is most often used along with Ajax calls that only update one or more elements on the page without reloading. Providing partial actions with the controller allows you to use the same partial template both for full-page rendering (by calling it from the template) and when subpage updates occur (from the action of the controller responding to Ajax calls). By default, the current layout is not used.

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

The implementation of the template works the same as the rendering of actions, except that it takes a path relative to the root of the template. 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 member name because you are using this as a template.

Hope it works!

+5
source

In the end (and I know this is a hack), but I created the partial name _partial_layout_wrapper in my layouts, which was an exact copy of the application layout / file and used this in my controller

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

it works, but of course it cannot be so.

+2
source

Could you post your entire controller? Rails displays layout / application.html.erb by default if no other layout is specified.

From the guides ror:

Partial layouts

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

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

This will look for the partial name _link_area.html.erb and display it using the layout _graybar.html.erb. Note that layouts for partial ones follow the same underline name as regular partial ones, and are placed in the same folder with the particle to which they belong (not in the master layouts folder).

Also note that it is explicitly stated: partial requires additional parameters when transferring, such as: layout.

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

+1
source

Source: https://habr.com/ru/post/975015/


All Articles