I am trying to pass a variable to render like this:
def monitor
@peripheries = Periphery.where('periphery_type_name=?','monitor')
render 'index', type: 'Monitor'
end
Here i want to use 'type' variable inside index view which looks like that:
<%= render 'some_partial', periphery_type: type %>
which also render something. But i want to use that 'type' variable
There are two ways to pass variables between the controller and views:
def monitor
@peripheries = Periphery.where('periphery_type_name=?','monitor')
@type = 'Monitor'
render 'index'
end
Rails will export any of the instance variables belonging to the controller to the view context. So you can call @type
in the view or any partial.
def monitor
@peripheries = Periphery.where('periphery_type_name=?','monitor')
render 'index', locals: { type: 'Monitor' }
end
Note that you need to pass a hash to the locals
option. You also need to pass the local variable to partial views:
<%= render 'some_partial', locals: { periphery_type: type } %>
Using locals is often a good idea since it encourages decoupling and can make your partials more reusable.
You can use redirect_to some_path(params: params)
In the monitor method, you can pass the type variable to the index view e.g.
def monitor
@type = 'monitor'
@peripheries = Periphery.where('periphery_type_name = ?', @type)
render 'index'
end
and in your view
<%= render 'some_partial', locals: {periphery_type: @type}
(Note this is not a good way to set up the type variable, I would need a better understanding of the class to suggest how to set that variable.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With