If post exist then display new menu item - ruby-on-rails

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>

Related

Add param to link_to show method

I have the current code in my traders.index.html file
<ul>
<% #traders.each do |trader| %>
<li><%= link_to trader.name, trader %></li>
<%end%>
</ul>
I want to add an extra parameter to be sent through, I tried
<li><%= link_to trader.name, trader, {:restricted => params[:s]} %></li>
But this doesn't send the parameter, whats the actual format of the link_to to get this done?
You can do:
<%= link_to trader.name, trader_path(trader, restricted: params[:s]) %>

How to render partial with different model data to other controller

At this moment I am at products/index page, I have set up layout so now at the left side I have menu where I can choose between multiple categories. These are links, so when I click on them I am redirected to categories controller with specific categegory.
But I wan't to just render category controllers action show with specific category.
Is that possible, I need just a tip, I don't need full answer :)
Thanks
in side bar
<%= link_to products_path(:category => cat.id) %>
in products#index
#category = Category.find params[:category]
in products/index.html.erb
<% render '/categories/category' if #category.present? %>
Couldn't find Category without an ID Its because off the this metod that generates link_to these categories, how can I merge this kind off functionallity ?
<ul id="menuks">
<li id="auctions">Atkritumu pārstrādes tehnika</li>
<% #children1.each do |o|%>
<li class="submenuks"> <%= link_to (o.name), o %></li>
<%end%>
</ul>
Thanks to #rhernando I found solution.
Controller
#category = Category.find_by_name(params[:category])
Side menu
<li id="auctions">Atkritumu pārstrādes tehnika</li>
<% #children1.each do |o|%>
<li class="submenuks"> <%= link_to (o.name), products_path(:category => o.name) %></li>
<%end%>

Create object from a block of search results Ruby on Rails

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!

Rails: how to exclude one item from an "each" method

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/

Rails 3.1 routing confusion

I am trying to create a link to a record in my application:
<ul class="presentation-box">
<% #presentations.each do |presentation| %>
<li><%= link_to "Presentation", presentations_path(#presentation) %></li>
<li><%= presentation.author %></li>
<% end %>
</ul>
With the following line in the routes file:
resources :presentations
root :to => 'presentations#index'
For some reason, when I click the link it's taking me to the Presentation index view. I believe it should be taking me to the show view of the individual record?
Am I missing something obvious?
Your link_to is incorrect.
presentations_path will actually point you to the index, you want presentation_path(presentation) to point directly to the resource.
Also, you can just do <%= link_to 'Presentation', presentation %> and Rails will build the correct path for you
Change it to presentation_path(presentation)

Resources