Rails: allowing a partial to only be rendered once
You could make use of the instance variables. However, you have to ensure that the instance variables is bounded to the views only and will not be crashed.
Something like:
# in your view
<% @widget_partial_rendered_popovers ||= [] %>
<% popover_partials.each do |pp| %>
<% if !@widget_partial_rendered_popovers.include?(pp) %>
<% @widget_partial_rendered_popovers << pp %>
display the popover
<% end %>
<% end %>
If you are able to collect the popover things early in the controller, it would be cleaner to just render all the popovers in a place. As I don't know what's your exact structure is, I could just give you what you have required.
how to render partial on everything except a certain action
Replace your render with this:
<%= render 'layouts/header' unless @disable_nav %>
Then you can simply set disable_nav
to true in any controller action you like:
def landing_page
@disable_nav = true
end
As a before_filter
, which I'd encourage over the above:
application_controller.rb
def disable_nav
@disable_nav = true
end
my_controller
before_filter :disable_nav, only: [:landing_page]
Rails render loads partial only
Well, finally solved my problem.
I replaced in my controller
render(:partial => 'users/downloads', locals: {user: user}, :layout => false) # rendering html:(
with something like
format.js {render(users/show.js.erb)} # should render js!
Rendering a partial N times instead of for each item in an array
When you do this...
{ ...
:smalls => [1],
...
}
...you still have an array: [1]
is an array with one element, the integer 1
(just like [1, 2]
is an array with two elements) . So first, you want to get rid of the arrays:
{ ...
:smalls => 1,
:mediums => 2,
:larges => 1,
...
}
For your view, when you use each
you're saying, "do this one time for each item in the array." Since you now have a number, not an array, you can use times
which, just like it sounds, means "do this N times":
<div id="small-field-set">
<% product[:smalls].times do %>
<%= render :partial => 'small', :locals => { :product => product, :small => "S" } %>
<% end %>
</div>
P.S. I'm not sure what the purpose of :small
in the :locals
hash is. Since your partial is called 'small'
I'm guessing you don't need this, or can easily change your partial to get rid of it.
Rails: Render a View (not a partial) From Within a View
Rendering a non-partial view inside another view isn't exactly the Rails Way™. Your current solution is probably better, and not an uncommon approach. Rename it _body, or something else appropriate, if you feel weird about the the partial name being the same as the action.
However if your view can be shared, as it seems like it could in this case, you could just make it a layout.
This is facilitated by the fact that, somewhat against the principle of least surprise, Rails will render an html
template for a js
action if no js
template exists. This means that you could remove both the js template, and the partial, and just create a layout entitled, for example, fadein.js.erb
:
# yourviews/show.html.erb
<div>Content!</div>
# layouts/fadein.js.erb
$("#main").fadeIn("<%= escape_javascript(yield) %>");
# YourController.rb
def show
# ...
respond_to do |wants|
wants.html
wants.js { render :layout => "fadein" }
end
end
Related Topics
How to Link to a Page with Page.Url Without the HTML Extension in Jekyll
Where Are Catch and Throw Useful in Ruby
How to Reduce the Quality of an Uploading Image Using Paperclip
Kernel_Require.Rb:55:In 'Require': Cannot Load Such File Error
How to Spec Methods That Exit or Abort
Error While Installing Iconv on Windows by Ruby2.0.0
Regex Negative Lookbehind in Ruby Doesn't Seem to Work
Execute Code Once Sinatra Server Is Running
How to Force To_Yaml to Output Long Strings in Literal Block Style
Ruby Block Taking Array or Multiple Parameters
Dangerousattributeerror in Omniauth Railscast Tutorial: Create Is Defined by Activerecord
Why Is Ruby's Loop Command Slower Than While True
Is /Etc/Irbrc Installed by Os X? Does Irb Read It
Ruby Code for Modifying Outer Quotes on Strings
Nokogiri Fails to Install on Os X
Can You Have Multiple Versions of a Gem in a Gemfile
Regular Expression to Match Only the First File in a Rar File Set
How to Fix Ruby Script Which Fails with Encoding Error: "\Xd8" on Us-Ascii