The difference between rendering and rendering partial and profitability

I read it from the Rails manuals, looked at the Micheal Hartel book and now I read it from the Rails View book, but still I'm confused :(

There is a _footer.html.erb file, so it is "partial" in the code that he wrote:

 <%=render 'layouts/footer' %> 

so I understand that when he sees this, he goes and inserts the HTML code for the footer here. OK ... Now a few pages later say:

 <%= render partial: 'activitiy_items/recent' %> 

therefore, WHY this time we have the word "partial" here, but we did not have it in the previous one?

And somewhere else I see <%= yield :sidebar %>

So, does this yield also embed HTML in its place? Well, isn't that what render did?

I was hoping that another programmer, instead of books, would explain this to me, maybe I will get it this time :)

+46
ruby-on-rails
May 29 '13 at 19:59
source share
3 answers

render and render partial:

  • render 'some_view' - short for render partial: 'some_view' .
  • render file: 'view' will look for the file view.html.erb and NOT _view.html.erb ( .erb or any other visualizer you use)
  • render will not accept additional local variables for the partial, you need to use render partial: as shown below:

     render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' } 

( http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables )

yield & content_for

  • yield commonly used in layouts . He tells Rails to put the contents of this block in this place in the layout.
  • When you do yield :something related to content_for :something , you can pass a block of code (view) to display the location of yield :something (see the example below).



A small example about profitability:

In your layout:

 <html> <head> <%= yield :html_head %> </head> <body> <div id="sidebar"> <%= yield :sidebar %> </div> </body> 

In one of your views:

 <% content_for :sidebar do %> This content will show up in the sidebar section <% end %> <% content_for :html_head do %> <script type="text/javascript"> console.log("Hello World!"); </script> <% end %> 

This will create the following HTML:

 <html> <head> <script type="text/javascript"> console.log("Hello World!"); </script> </head> <body> <div id="sidebar"> This content will show up in the sidebar section </div> </body> 



Messages that may help :

  • Built-in Ruby - Render vs. Yield?
  • Rendering @ object and local residents versus rendering: partial
  • Rails: about profitability

Links to documentation and manuals :

+96
May 29 '13 at 20:07
source share

About render, render: partial and yield

  • render: template and render: partial are two files in rails ..




    render: the template is basically created according to the action with the syntax demo.html.erb

    render: partial is reused and called from different views, shared between many pages in the application, and the syntax is _demo.html.erb

  • profitability and rendering.




An exit is a way to call a block of code with its exit, but the rendering will include a partial page template where it is called. In rails, the output is mainly used in the layout, while the render is used in actions or their templates.

+3
Jun 13 '13 at 9:24
source share

Some developers think of redirect_to as some kind of goto command, moving execution from one place to another in your Rails code. This is not true. Your code stops working and is waiting for a new request for the browser. It just happens that you told the browser what request it should do next by sending an HTTP 302 status code.

Consider these steps to see the difference:

 def index @books = Book.all end def show @book = Book.find_by(id: params[:id]) if @book.nil? render action: "index" end end 

There will probably be a problem with the code in this form if the @book variable is nil. Remember that render :action does not run any code in the target action, so nothing will set up the @books variable, which is likely to require an index. One way to fix this is to redirect instead of rendering:

 def index @books = Book.all end def show @book = Book.find_by(id: params[:id]) if @book.nil? redirect_to action: :index end end 

Using this code, the browser will make a new request for the index page, the code in the index method will be launched, and everything will be fine.

The only drawback of this code is that the browser requires a two-way trip: the browser requested a show action with / books / 1 and the controller detected that there are no books, so the controller sends a redirect response 302 to the browser telling it to go to / books /, the browser agrees and sends a new request back to the controller, now requesting the index action, the controller then receives all the books in the database and displays the index template, sends it back to the browser, which then displays it on the screen.

Although this added delay may not be a problem in a small application, you need to think about whether response time is a problem. We can demonstrate one way to deal with this contrived example:

 def index @books = Book.all end def show @book = Book.find_by(id: params[:id]) if @book.nil? @books = Book.all flash.now[:alert] = "Your book was not found" render "index" end end 

This will reveal that there are no books with the specified ID, fill the @books instance variable with all the books in the model, and then directly render the index.html.erb template, returning it to the browser with a flash to tell the user what happened.

-one
Feb 26 '16 at 13:49
source share



All Articles