I'm using the Enki blogging gem as a type of content management system. It allows you to create posts and pages. It automatically generates two pages (Home and Archives). I've also created two other example pages, Services and Products, and will create many more. Therefore, when I want to list all the pages on the home page, I do this
<% page_links_for_navigation.each do |link| -%>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
Home
Archives
Services
Products
I may want to create more pages in the future, so it's better to loop over all the pages like this rather than hardcode the url for each page.
But how would I change that code if I wanted to exclude one of those pages (i.e. archives). Enki automatically generates that page and doesn't give me an option to delete it. Moreever, I don't want to delete Archives, because I want to use it where I post link to blog posts.
So, in short, how would I exclude one particular page from this code
<% page_links_for_navigation.each do |link| -%>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
The url for Archives is localhost:3000/archives
another way
<% page_links_for_navigation.each do |link| -%>
<% next if link.name == 'Archives' %>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
<% page_links_for_navigation.each do |link| -%>
<% if link.name != 'Archives' %>
<li><%= link_to(link.name, link.url) %></li>
<% end %>
<% end -%>
or use page_links_for_navigation.reject {|page| page.name == 'Archives'}.each
Edit:
to add more pages do !['Archives', 'Home'].include? link.name or just add the ones you want to include and remove !
read
http://www.humblelittlerubybook.com/
http://www.ruby-doc.org/docs/ProgrammingRuby/
Related
I am new to Ruby on Rails and am having a difficult time figuring out how to not show a div on more than one page. Currently, I've only been able to make the following work for a single page:
<% if signed_in? %>
<% unless current_page?(account_setup_path) %>
<!--job seeker options-->
<% if current_user.job_seeker? %>
test
<% end %>
<!--end job seeker options-->
<!--employer options-->
<% if current_user.employer? %>
<% end %>
<!--end employer options-->
<% end %>
<% end %>
Any help will be much appreciated! Thanks in advance!
Since you want a div to not be shown on multiple pages you have several options. First, if the div is only meant to be shown on pages with a certain controller you'll want to move that div into a partial and reference it from all the associated views. If you want in only shown on one page you should put it in the view directly. If you need it shown on several different pages accross your app. you can simply check if the controller in your params hash matches. For example:
#I want this div shown on any pages handled by my `Admins` and `Users` controllers.
<% if params[:controller] == 'admins' || params[:controller] == 'users' %>
div here
<% end %>
This will add overhead to maintenance so you should think hard whether this div should be in a partial, in a specific view, or in the layout/application file etc.
I have the following situation in my rails project
application.html.erb:
...
<%= render :partial => 'layouts/sidebar' %>
<%= yield %>
...
_sidebar.html.erb:
<% #groups.each do |group| %>
<li><%= link_to group.name, "#" %></li>
<% end %>
Sidebar consists of a list of groups which doesn't change often. That's why i don't want to query the list in DB every time I go to another page in content part (yield). Is there a way how can i preserve the list throughout several pages? I KNOW about the sessions and caches, but maybe there is a better solution.
Thanks
I am using the ancestry gem, and what I want to do is always detected all the related posts to the current post.
So my Post has_ancestry.
In a related posts section of my view, I have this:
<% if #post.has_children? %>
<p>
<ul>
<% #post.children.each do |child| %>
<li><%= link_to child.title, post_path(child)%></li>
<% end %>
</ul>
</p>
<% else %>
<p>
There are no related posts.
</p>
<% end %>
</div>
While that approach is fine, it just checks one use case (i.e. if this post has a child). What I want to happen though is when the person clicks through to the child, on that show page it should show this post as a related post too.
In effect it would be a parent, but I don't want to have to do if post.has_parents?, then if post.has_children?, if post.has_siblings?, etc.
Ideally, I would just like for any relatives to be detected, and if there are relatives I want to loop through them all and display them all in a homogenous way.
How do I do that?
I want to be able to display a different menu item if a post exists in a certain page, but unsure of how to best call the controller method in a partial.
I have a career page and if there is a post within that page I would like to display 'NEW' next to the career item in my menu, if no post exist then I just want it to display 'Careers'.
_header located in views -> layouts
<% if Careers.exists?(:id) %>
<li><%= link_to 'Careers NEW', careers_path %></li>
<% else %>
<li><%= link_to 'Careers', careers_path %></li>
<% end %>
try this:
<li><%= link_to "Careers #{(Career.present?)? 'NEW' : ''}", careers_path %></li>
I'm building a Rails application that allows the user to create a Book object based on search results from the Google Books api. I have a controller that handles searching the api, using the GoogleBooks gem and displays the results in a list. I'm struggling to figure out a way to pass book information from a single search result into my Create action in the Books controller.
My search controller takes params from my search form and creates a variable, #results, that I'm calling in a 'search' view. Here is my search controller:
class SearchController < ApplicationController
def search
#results = GoogleBooks.search(params[:search])
end
end
My view looks like this:
<h1>Search Results</h1>
<% #results.each do |result| %>
<ul>
<li><%= result %></li>
<li><%= result.title %></li>
<li><%= result.authors %></li>
<li><%= result.isbn %></li>
<li><%= result.description %></li>
</ul>
<% end %>
The problem that I'm having is that I'm not sure how to pass individual result data on to my Book controller's 'create' action to generate a new book in the database. I don't think I can pass 'result.title' or 'result.author' to the Book controller for example because they aren't instance variables and there is also no way to distinguish between each result.
My page source for search results looks like this, if that is any help.
<h1>Search Results</h1>
<ul>
<li>#<GoogleBooks::Item:0x007ff75d2bd138></li>
<li>Kansha: Celebrating Japan's Vegan and Vegetarian Traditions</li>
<li>Elizabeth Andoh</li>
<li>9781607743965</li>
<li>The celebration of Japan’s... </li>
</ul>
<ul>
<li>#<GoogleBooks::Item:0x007ff75d2bc148></li>
<li>Advanced Energy Saving and Its Applications in Industry</li>
<li>Kazuo. Matsuda, Yasuki. Kansha, Chihiro. Fushimi</li>
<li>9781447142072</li>
<li>The conventional approach for... </li>
</ul>
I would like to use a button to allow a user to 'select' the book and then pass on the book's information to the create action. I'm thinking I need to do something like this with button_to:
<%= button_to 'Create Book', book_path, :method => :post %>
But how do I get the a single book's data to the create action?
Depending on your overall layout, I suggest start by building an in-line form for each Book result. You can do this with hidden fields. As mentioned by gregates, it's hard to provide the complete answer, but this might get you going in the right direction.
In your SearchController:
def search
#results = GoogleBooks.search(params[:search])
#books = []
#results.each do |result|
#books << Book.new
end
end
In your View:
<% #results.each_with_index do |result, index| %>
<%= form_for(#books[index]) do |f| %>
<ul>
<li><%= result.title %></li>
<li><%= result.authors %></li>
<li><%= result.isbn %></li>
<li><%= result.description %></li>
</ul>
<%= f.hidden_field :title, value: result.title %>
<%= f.hidden_field :authors, value: result.authors %>
<%= f.hidden_field :isbn, value: result.isbm %>
<%= f.hidden_field :description, value: result.description %>
<%= button_to 'Create Book', controller: 'books', action: 'create' %>
<% end %>
<% end %>
Again, depending on your overall design, there's probably a more elegant approach but hopefully this will provide some inspiration.
Good luck!