I'm trying to render a partial on home page. Code is a follows:
<%= render :partial => 'spree/shared/slider' %>
The thing is I want it to render/show the partial only if I'm on the home page .To summaries for a particular page/pages only.
Can anyone help me with this? Thank you.
You can use a conditional for deciding whether the partial should be rendered:
<%= render :partial => 'spree/shared/slider' if #slider %>
The condition could be an instance variable (e.g. #slider) which can be set from the view (when not set, it remains nil by default):
<% #slider = true %>
Now the partial will only be rendered if #slider has been explicitly set to true in the view or in the controller.
Related
How do I render a partial view via its controller when loading a separate view in Ruby on Rails?
Thanks for your help in advance
Problem: I have a show.html.erb for my Patients controller, when this loads I have a series of tabs, one of which is a list of notes added to that patient record. Notes is a separate model. I want to load the list of notes when loading show.html.erb.
I can use the code below to render the view directly and can pass variables in this way, but I would rather go through the controller and deliver notes/_list.js.erb, via the Patients Controller.
<%= render "notes/list", :remote => true %>
I have a solution that loads the partial via a link_to but because the link is in a tab it then breaks my tab javascript, and would rather just load the information on initial page load rather than have the user interact with an element.
<%= link_to "Notes".html_safe,{:controller => "patients", :action => "notes", :remote => true } %>
It needs to be in a partial as I am then using pagy to paginate the results and if I display it via a controller dependent partial then the pagy navigation links function as expected.
Relevant Routes
get 'patients/:id/show' => 'patients#show', as: :show_patient
get 'patients/:id/notes' => 'patients#notes', as: :notes_patient
Relevant Patients Controller
def notes
#pagy, #notes = pagy( Note.where(:patient_id => params[:id]).reverse_order, items: 2 )
respond_to do |format|
format.html { render "notes/list" }
format.js { render "notes/list" }
end
end
notes/list.js.erb
$('#notes-list').html('<%= escape_javascript(render('notes/list')) %>');
notes/_list.html.erb
<% #notes.each do |note| %>
<%= render partial: note %>
<% end %>
<%== pagy_materialize_nav(#pagy) %>
<span class="pagy-info"><%== pagy_info(#pagy) %></span>
I believe you can do something like this in the controller:
# or whatever this partial is called
#notes_rendered = render_to_string "notes/list"
And then use it in the view you want.
Im trying to bring a layout in my html page by
render layout 'flatty'
thing is this loads the whole flatty layout. In flatty.html.erb it renders _header,_footer and also _sidebar.html.erb
I dont want to load _sidebar.html.erb in this particular page.
So how should i render this?
thing is this loads the whole flatty layout. In flatty.html.erb it renders _header,_footer and also _sidebar.html.erb I dont want to load _sidebar.html.erb in this particular page
Why do you want to use same layout if you have so many changes? Why not make a partial which you could render in both cases. Make a new partial, lets say _common.html.erb, render it in your flatty layout and view of the action in which you want to use it.
#flatty.html.erb
<%= render "common" %>
<%= render "sidebar" %>
#some_action_name.html.erb
<%= render "common" %>
If you still want to use same layout in both cases then you ca use rails 4 controller_name and action_name helpers in your layout and selectively render sidebar and other partials in your layout:
#flatty.html.erb
<%= if controller_name == "some_controller_name" && action_name == "some_action_name"
<%= render "sidebar" %>
<% end %>
Maybe in your controller action, you can have a flag indicating the sidebar should not be rendered. Then, in your flatty.html.erb file, check for the flag variable before you render the _sidebar.html.erb.
For example, if you have a controller action called flatty, add an instance variable, #disable_sidebar, to act as your flag.
def flatty
#disable_sidebar = true
# Your other code
render layout: 'flatty'
end
Then, in your flatty.html.erb, add a conditional before your render for your sidebar (note the ! negation in the if statement:
<% if !#disable_sidebar %>
<%= render "layouts/sidebar" %>
<% end %>
Alternatively, in your flatty.html.erb you can also check for the controller and action values in your params hash, and then don't render your sidebar if it matches that controller's action:
<% if params[:controller]!="YOUR_CONTROLLER" and !params[:action].eql? "flatty" %>
<%= render "layouts/sidebar" %>
<% end %>
I have a resource called Exercises in my application. I currently have a partial called _exercise.html.erb that I use to render them. I have an outlying case where I'd like to render them in a much different way. Can I make another partial for exercises that has this other format and still be able to use <%= render #exercises %>?
If not what is the best approach? Should I out a variable in the controller that tells the partial which layout to use, this would have both layout in one file and one if to decide. Or is there some better way?
If you'd like to use business logic to determine when to show what partial for your #exercises collection you should use the to_partial_path method in the Exercise model to define that. See #4 in this post: http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/
Or, if this is more of a view-related decision (i.e. one view will always use the regular _exercises.html.erb and another view would always use e.g. _alternate_exercises.html.erb) then you can specify as such:
<%= render partial: 'alternate_exercises', collection: #exercises, as: :exercise %>
This will render the _alternate_exercises.html.erb partial once for each item in #execrises passing the item in to the partial via a local_assign called exercise.
In this case, I suppose you have two options:
1) Put the conditional code inside of _exercises.html.erb
eg.
<% if #exercise.meets_some_condition %>
you see this stuff
<% else %>
you see other stuff
<% end %>
This way, you can still make use of <%= render #exercises %>
2) Otherwise, your other option is to have separate partials and render them outside.
eg.
<% #exercises.each do |exercise| %>
<% if exercise.meets_some_condition %>
<%= render "exercises/some_condition_exercise" %>
<% else %>
<%= render "exercises/exercise" %>
<% end %>
<% end %>
This is the best approach for rendering partial. You can wrap that partial with if else statement in your code. Here is my example
rendering with form called _victim.html.erb
<%= render :partial => "victim", :locals => {:f => f }%>
rendering without form
<%= render :partial => "victim"%>
I have an index view for a model(nested ) that never gets called from the model, but is rendered from a couple different models. One view can render the index with either JS or html. With JS is it in a #related div in the show view and a data-remote link. Another option uses the same controller action to render it only has html.
All was fine until I added Kaminari paging. In the full page view, there was no #related div so paging didn't work. I broke out the guts into a partial and added a #related div and rendered the partial. From js I just rendered the partial from JS. That worked fine in the full page view, but in the show page it would render the partial, but the links didn't work, looks like it renders the entire show page. Clinking on another tab and then going back to the Progressions tab loads the partial and all works fine. It is just on the initial load of the show page that I can't get the page links to work.
I can load another tab that does not use paging first and solve my problem, but this was kind of the main information that you go to this page for.
Any ideas?
EDIT Request for code
The controller action method. The index method in this controller also sets #progressions
def progressions
authorize! :read, Progression
#stage = Stage.find(params[:id])
#progressions = #stage.progressions_by_score.page(params[:page]).per(20)
if params[:status] && !params[:status].blank? && #progressions
#progressions = #progressions.where(status: params[:status]).page(params[:page]).per(20)
end
respond_to do |format|
format.js
format.html {render :template => "progressions/index"}
end
end
The progressions.js.erb file in the stages view
$("#related").html("<%= escape_javascript(render('progressions/index'))%>");
The relations div in the show view. This is pretty much my scaffold template where I display or link to related information. The <div id="related"> is where any date-remote links will load the data. On initial load it loads the _index partial.
<div class="relations">
<span class="navspan">
<%= link_to 'Progressions: Status ->', progressions_stage_path(#stage), :'data-remote' => true,
:onclick => '$("#progression_status").val("")' %>
<span class="huh">
<%= hidden_field_tag :stage_id, params[:id]%>
<%= select_tag :progression_status, options_for_select(Progression.statuses["all"], params[:status]),
:prompt => 'All', :class => 'f-left' %>
</span>
<%= link_to 'Assessors', assessors_stage_path(#stage), :'data-remote' => true %>
<%= link_to 'Events', events_stage_path(#stage), :'data-remote' => true %>
<%= link_to 'Select', select_stage_path(#stage) if can? :select_candidates, #stage %>
<%= link_to 'Ad Mgmt', edit_ad_stage_path(#stage) if can? :manage_ad, #stage %>
</span>
<div class="f-clear"></div>
<div id="related">
<%= render "progressions/index"%>
</div>
</div>
The index.html.haml file
#related
= render "progressions/index"
The _index.html.haml file is just an index table listing the progressions but it does contain:
= paginate #progressions, :remote => true
Shortly after posting some code, I went back in my memory and used javascript to load the related div. I've tried to stay away from javascript, but in this case I added this to the end of the page after adding :id => "status_id" to the progressions link:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#status_link").trigger("click");
})
</script>
While this fixes my problem, I still don't understand why the initial html response does not respond to the page links in the related div. I'll leave that as something else to learn. I'll put it in coffescript after I figure out how to have fire only on initial load of the show page.
I have a view that has a block corresponding to a partial view (very simple).
<td WIDTH ="70%">
<%= render "partial_1" %>
</td>
Now, When the user clicks certain button, I make a Ajax call to my controller, and after my business logic is done, I want to return to the same view but rendering a different partial view.
def ajax_call
....
render :layout => "administracion"
end
I tried changing my <%= render "partial_1" %> for <%= yield %>
and in my controller: render :layout => "administracion", :partial => "mypartial"
but when I do this, only the partial is rendered, the other elements of my original view are lost.
What should I do?
I'm using rails 2.3.9
Thank you!
Look at the example here.
In a few words, you need to create a js.erb view and include your partial this way:
$('#your_container').html('<%=escape_javascript render("your_partial") %>');