Ruby / Rails: difference between "@item" and "item" in a view

I have a view that can be displayed from a controller or as partial from another view.

In all the code I read, the controllers assign the @item instance variable and then call the view. On the other hand, when rendering it as partial, it gets the parameter "item".

So, all my views start as follows:

item ||= @item

Not very dry. Is there a better way I'm missing?

+3
source share
1 answer

@item - - ​​ . Rails "" . , :

class Foo
  def initialize
    @bar = "Yay!"
  end

  def show_bar
    puts @bar
  end
end

Foo.new.show_bar Yay!, @bar .

item, , . , . Rails .

, 1:1 , , . - () . , , ; , , .

, , " , " . , , :

<%=render :partial => "item_partial", :locals => {:item => @item} %>

item_partial item, @item.

, "" .

+5

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


All Articles