I have a base layout in /views/layouts/application.html.haml
%html
%head
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= render 'layouts/header'
= yield
= render 'layouts/footer'
In all my views rendered, it inherits from the above with the header and footer.
How can I render a page that doesn't inherit from /views/layouts/application ? Or just render partially by omitting the header and footer, but still include the stylesheet and javascript?
You could render the header and footer for all views except a specific view by checking the controller_name and action_name. e.g
%html
%head
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= render 'layouts/header' unless controller_name == 'CONTROLLER' && action_name == 'ACTION'
= yield
= render 'layouts/footer' unless controller_name == 'CONTROLLER' && action_name == 'ACTION'
So if you put users instead of CONTROLLER and show instead of ACTION the header and footer would not display for the user's show action.
Related
This is my first time trying to implement an Ajax call in rails 3, though I am using the .load function ( I still hope this is Ajax otherwise im understanding this incorrectly)
So i have a search form that returns results via a get request which renders on a different page, i would like the results to appear on the same page as the search form
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |select| %>
<%= label_tag :search, "Enter Keywords Here" %>
<%= text_field_tag :search, params[:search] %>
(I have shortened the form)
<%= submit_tag "Search", :class => "searchbutton" %>
<% end %>
Jquery/Ajax call
$(document).ready(function() {
$('.searchbutton').click(function() {
$('#searchres').load('shared/searchresults');
});
});
View
<h3>Search Recipes here</h3>
<%= render 'shared/searchrecipes' %>
<div id ="searchres">
</div>
What am i doing wrong?
Due to this being an AJAX call, you need to add remote: true besides method: :get, getting an html parameter hash like this:
{:method => 'get', :remote => true}
When Rails finda remote call, it prevents the default automagically. Otherwise, you would need to modify you javascript like this:
$(document).ready(function() {
$('.searchbutton').click(function(evt) {
$('#searchres').load('shared/searchresults');
evt.preventDefault();
});
});
I am rendering a partial like so:
<% #pages.each do |page| %>
<%= render 'layouts/pagewithchildren', :locals => { :page => page } %>
<% end %>
But when i try to access a variable in page i am getting the error:
undefined local variable or method `page'
I am accessing the variable like:
<%= page.title %>
So what else do I need to do?
i'm not 100% sure but isn't it either
<%= render 'layouts/pagewithchildren', :page => page %>
or
<%= render :partial => 'layouts/pagewithchildren', :locals => { :page => page } %>
?
You have to explicitly specify partial, otherwise, Rails will treat locals as a params hash, you can access locals[:page] but not page variable directly in your partial.
Change your code to:
<%= render partial:'layouts/pagewithchildren', locals: {page: page} %>
I have a Rails 3 application that is using the 960 grid CSS layout(s). There are a couple of different views that stretch in width and I am trying to come up with a good way to dynamically change out those classes.
For Example:
My Devise Controllers (Sessions, Passwords, etc) all use a certain class to restrict the width to 340px, while most of my other controllers use another class to restrict the width to 540px
So my 340px layout uses class names grid_6 push_5, while my 540px layout uses grid_10 push_3
Anyway to grab the accessed controller in the application_controller? My thinking is to just get the controller and have a switch statement that sets the class names in a helper_method.
Thoughts?
I was able to figure it out, thanks to this post: Determine the requested Controller in - ApplicationController
Here is how I ended up doing it (application.html.erb):
<%= render :partial => 'common/content_container', :locals => { :controller => params[:controller] } %>
content_container partial:
<% case controller
when "sessions", "passwords"
container_div_grid_number = "6"
container_div_push_number = "5"
else
container_div_grid_number = "10"
container_div_push_number = "3"
end
%>
<div class="grid_<%= container_div_grid_number %> push_<%= container_div_push_number %> ">
<div class="top_<%= container_div_grid_number %>"></div>
<div class="middle_<%= container_div_grid_number %>">
<%= yield %>
</div>
<div class="bottom_<%= container_div_grid_number %>"></div>
</div>
I'm trying to generate dynamic content in my views.
in my application.html.haml
!!!
%html
%head
%title YieldUsage
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= yield
= yield :head
= yield :scripts
= yield :name_section
and my helper is like;
def name_section_form
content_for :name_section do
label_tag(#post.name)
end
end
and my view;
%article
= name_section_form
But it renders;
<body>
<article>
</article>
<label for="asfd">Asfd</label>
</body>
Why is it happening? Bug or something? Any ideas?
Thanks.
Çağdaş.
Your view gets dumped into the yield. The method sets the content for a specific yield. Remove the content_for in your helper.
Hey,
I'm working on a page having a nested layout. first I have the applicationlayout with my "mainmenu" now I want to add a second menu only on this page. I got this working via
<% render :partial => "mypartial", :layout => 'navigation' %>
this adds my second navigation to the form and renders a partial.
At this point I try to distinguish between two different partials. so my file looks like this
<% if :passed_text == "page1" %>
<%= render :partial => "mypartial1", :layout => 'navigation' %>
<% else %>
<%= render :partial => "mypartial2", :layout => 'navigation' %>
<% end %>
my navigation is as follows:
<%= link_to "Mypartial1", partial_path, :passed_text => :page1 %>
<%= link_to "Mypartial2", partial_path, :passed_text => :page2 %>
<%= yield %>
but it ignores my parameters. I guess I'm missing something basic, but all this is new to me.
thanks for your help
okay I found an answer:
first I have to check for:
params[:passed_text]
instead of :passed_text
secondly passing the parameters has to be in brackets
partial_path( :passed_text => :page1)
this works fine