Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 3 rendering view without action

I defined the routes for a particular action and created a link. I created the corresponding views too but did not code define the controller action method. Still the view is rendered on clicking the link. That is the view is rendered without the action actually being present.

Any explanations ?

like image 551
AshwinKumarS Avatar asked Nov 22 '13 06:11

AshwinKumarS


People also ask

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.

How do you render a partial?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is Local_assigns?

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.

What is render partial in Ruby on 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.


2 Answers

Yes, the view will be rendered even if no corresponding action is present, it will work as routes are defined for the same. But this is not a good practice!

like image 68
Rajdeep Singh Avatar answered Sep 19 '22 14:09

Rajdeep Singh


For Rails to render a view, you'll need to have defined a controller (not necessarily with a corresponding method), a route that references the view and the view. Adding a method to the controller is only necessary if you need to provide data to the view.

There's a pretty thorough explanation of this in http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action

like image 25
Glen Aultman-Bettridge Avatar answered Sep 19 '22 14:09

Glen Aultman-Bettridge