Ruby on Rails: conditionally display a partial
Ruby allows you to do nice things like this:
<%= render :partial => "foo/bar" if @conditions %>
To make this a bit easier to read and understand, it can be written as:
<%= render(:partial => "foo/bar") if @conditions %>
render
is a function, and you pass it a hash that tells it which partial to render. Ruby allows you to put things on one line (which often makes them more readable and concise, especially in views), so the if @conditions
section is just a regular if statement. It can also be done like:
<% if @conditions %>
<%= render :partial => "foo/bar" %>
<% end %>
Edit:
Ruby also allows you to use the unless
keyword in place of if
. This makes code even more readable, and stops you from having to do negative comparisons.
<%= render :partial => "foo/bar" if !@conditions %>
#becomes
<%= render :partial => "foo/bar" unless @conditions %>
Correct way of conditional rendering within a partial in Rails?
Yes, you are on the right track. You can do something like
<%= render 'partial_name', limited: true %>
and then in the partial:
<% limited ||= false %> # More explicit as to what default value is
<tr>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price, unit: '€') %></td>
<% unless limited %>
<td class="remove_item"><%= button_to 'Remove', line_item, method: :delete %></td>
<% end %>
</tr>
Conditional Rendering of partial in Rails
There is a helper method in your views called controller_name ActionController::Metal
This would probably allow you to trigger the partial based upon the controller you're in.
<%= render :partial => "pages/partials/sub_nav" if controller_name == "company" %>
Note there is also a helper called action_name that allows you to check against the current action too. So you could combine them.
<%= render :partial => "pages/partials/sub_nav" if controller_name == "company" || action_name == "company %>
Of course you'll probably want to roll this if statement up into a helper method in your ApplicationHelper to DRY up your view
Rails: Conditional path in form partial
You can post local variables into the partial. Something like the following would work:
new.html.erb
<%= render partial: '_form', locals: {url: user_wikis_path, method: :post} %>
edit.html.erb
<%= render partial: '_form', locals: {url: user_wiki_path(@user, @wiki), method: :post} %>
_form.html.erb
<%= simple_form_for [@user, @wiki], url: url, method: method do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>
Or you could set @url and @method as instance variables and access them that way.
Controller
def new
@method = :get
@url = user_wikis_path
...
end
def edit
@method = :post
@url = user_wiki_path(@user, @wiki)
...
end
_form.html.erb
<%= simple_form_for [@user, @wiki], url: url, method: method do |f| %>
...
Or you could have a conditional in the form that accesses the current action, if you use action_name in the view it'll return the name of the current action.
How to conditionally show a partial in Rails
you can use render (this is the reference), and for your code here is the sample
def showdata(summary)
blah = 'test'
if summary
render('partial1', locals: { blah })
else
render('partial2', locals: { blah })
end
end
Rails 5: partial view with condition and locals
As far as I know you can't render the form with locals from the controller. Is there any reason why you would want to use a local? - these are normally used when rendering partials to pass local variables from one view to the partial, for purposes of reuse. for example...
<%= render partial: 'form', locals: { my_local_var: my_local_var } %>
I think what you actually want to do is set an instance variable from within the controller that is accessible during the rendering process. For example...
if @roles.any? { |role| role.creater_dashboard? || role.deleter_dashboard? }
@creator_deleter = true
elsif @roles.any? { |role| role.viewer_dashboard? }
@viewer = true
else
redirect_to users_path
end
and then you can test for that in your view
<% if @creator_deleter %>
How to render different show templates conditionally
Just simple render works for you, you can use below code:
Just render with HTML file name if file is in same controller's view
if @post.video?
render 'video_show'
else
render 'artical_show'
end
Rails how to render a conditional partial
Since you can tell rails to render :partial=>string, you can do string replacement inside that string. So, if @widget is the variable name:
<%= render :partial => "widgets/#{@widget.type}", :locals => {:widget => @wall} %>
Related Topics
How to Disable a Form Submit Button "A Là Ruby on Rails Way"
How to Elegantly Symbolize_Keys for a 'Nested' Hash
Run a Cli Thor App Without Arguments or Task Name
Ruby on Rails User Registration Using Rest API Call and Devise
How to Get Rspec to Run All Tests Nested Under a Folder
Convert Hash to Openstruct Recursively
Ruby - How to Write a New File with Output from Script
How to Print Something Without a New Line in Ruby
How to Do Case-Insensitive Order in Rails with Postgresql
Rails 3 - Best Way to Handle Nested Resource Queries in Your Controllers
Dynamically Create Class Attributes with Attr_Accessor
Decoding Facebook's Signed Request in Ruby/Sinatra
How to Detect User Agent in Rails 3.1
Rails 4 Update Nested Attributes
Safe Navigation Equivalent to Rails Try for Hashes
Is There a Workaround to Open Urls Containing Underscores in Ruby