Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails render of partial and layout in controller

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 to true that differentiates the two forms.

Upon form validation I would like the correct form to render (previously it was going back to the default form no matter what form i was using).

I am having an issue where just the partial is being rendered (obvious as i am only rendering the partial), but I need the layouts/application file to be rendered aswell.

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 have 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 it looking for a partial _application when I specified layout and how can i get the correct layout to be applied please

Thanks

Edit

Reading through the documentation it says

"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)."

But i want the default layout to be applied

like image 866
Richlewis Avatar asked Sep 09 '14 07:09

Richlewis


People also ask

What is render partial in Rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

How can you tell Rails to render without a layout?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

What is the difference between render and redirect in Rails?

There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.

What is the reason for having partials in Rails?

A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts. For example, you might have a login form that you want to display on 10 different pages on your site.


1 Answers

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 !

like image 57
Deepak Kumar Jha Avatar answered Oct 08 '22 04:10

Deepak Kumar Jha