How do I link to the particular ID of a model? - ruby-on-rails-3

I have a search controller and index page:
# search/index.html.erb
<% #user_prices.each do |up| %>
# This needs to go to particular ID of UserPrice.
<%= link_to show_user_price_path do %>
<%= up.product_name %>
<% end %>
<% end %>
----------------------------------------------------------
class SearchController < ApplicationController
def index
#search = UserPrice.search do
fulltext params[:search]
paginate(:per_page => 5, :page => params[:page])
end
#user_prices = #search.results
end
end
Above you can see on my search form I am trying to link the product name to that particular UserPrice.How would I do this?
Thanks.

<%= link_to up.product_name, show_user_price_path(up) %>
That should do?

Not exactly sure why you're using the block form of link_to, if you check the link_to documentation there is an easier way to use the function to get what you want.
<% #user_prices.each do |up| %>
<%= link_to up.product_name, show_user_price_path(up) %>
<% end %>

Related

Rails search form for multiple column in a table

I have a simple rails search form that will select rank on a column in which 3 different ranks in a table. But I cannot extend plural ranks.
Table Food: value1, rank_value1, value2, rank_value2, value3, rank_value3
View /foods/index.html:
<%= form_tag foods_path, :method => 'get' do %>
<p>
rank_value1
<%= text_field_tag :rvalue1, params[:rvalue1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Model food.rb:
def self.search(search)
if search
Food.where(["rank_value1= ?", "#{search}"])
else
Food.all
end
end
Controller foods_controller.rb:
def index
#foods = Food.search(params[:rvalue1])
end
To extend two ranks, I tried codes below but it did not work.
View /foods/index.html:
<%= form_tag foods_path, :method => 'get' do %>
<p>
rank_value1
<%= text_field_tag :rvalue1, params[:rvalue1] %>
<%= text_field_tag :rvalue2, params[:rvalue2] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Model food.rb:
def self.search(search1,search2)
if search
Food.where(["rank_value1= ? and rank_value2=?", "#{search1}", "# {search2}"])
else
Food.all
end
end
Controller foods_controller.rb:
def index
#foods = Food.search(params[:rvalue1], params[:rvalue2])
end
Any suggestions as to search with data in multiple columns?
Hey for searching with multiple values you can use Ransack gem. Here you can see the video tutorial and github link for implementation.
github: https://github.com/activerecord-hackery/ransack
video : http://railscasts.com/episodes/370-ransack
Hope you are looking for this only.

Rails 3 - Ransack - check_box_tag

Listing Model - belongs_to :area
Area Model - has_many :listings
I'm trying to implement a search using Ransack with check boxes; where user checks selected areas, search returns all the listings of the areas selected.
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq][]', area.id) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
Console output:
Parameters: {"utf8"=>"✓", "q"=>{"area_id_eq"=>["1", "2"]}, "commit"=>"SEARCH"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `to_i' for ["1", "2"]:Array:
Just not sure how to implement it to accept multiple check box values.
Instead of using "area_id_eq", use "area_id_any". You'll also want to check to make sure that your parameters are selected:
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq_any][]', area.id, (params[:q][area_id_eq_any].include? area.id.to_s) ? true : false ) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>

Rails: dynamically naming instance variables

Just wondering how to dynamically name (say, in a loop) a series of instance variables. Something like this:
<% #current_issue.articles.each_with_index do |a, i| %>
<% i += 1 %>
<%= f.collection_select("article#{i}", #articles_hash1, :first, :last) %>
<% #articles1.each do |r| %>
<%= link_to(image_tag(r.image.url(:large)), r.image.url(:large), :id => 'article'+i.to_s+'_thumb'+r.id.to_s) %>
<% end %>
<% end %>
Where instead of #articles_hash1 it'd be #articles_hash[i]. I'm just not sure how to achieve that.
Cheers!
<% #current_issue.articles.each_with_index do |a, i| %>
<%= f.collection_select("article#{i}", #articles_hash[i], :first, :last) %>
<% #articles_hash[i].each do |r| %>
<%= link_to(image_tag(r.image.url(:large)), r.image.url(:large), :id => 'article'+i.to_s+'_thumb'+r.id.to_s) %>
<% end %>
<% end %>
I didn't check for any syntax issues, but this should do.
For anyone struggling with this, do yourself a favour and build an array of the instance variables in question, i.e.:
#articles = (1..3).to_a.map { |i| Article.all_articles(i).reverse }
And then loop through that array, i.e.:
<% #articles.each_with_index do |a, i| %>
<% i += 1 %>
<%= f.collection_select("article#{i}", #articles[i-1], :id, :name) %>
<% end %>

Rails search functionality

I am taking a rails class at my University and I am trying to create a search form which will show the results on the same page rather than show a different page of results. Is this something simple to do? I am creating a museum app with artifacts for each museum but I want the user to search artifacts from either page.
On my routes.rb I have
resources :artifacts do
collection do
get 'search'
end
end
On my museum index I have the code below that he gave us but not sure how to tweak the get routes for the same page.
<%= form_tag search_artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
<% if #artifacts %>
<p> <%= #artifacts.length %> matching artifacts. </p>
<h2> Matching Artifacts </h2>
<% #artifacts.each do |a| %>
<%= link_to "#{a.name} (#{a.year})", a %><br />
<% end %>
<% end %>
Yes, this is easy. Just have the index page return the search results if params[:search_text] is present - this way you don't need a new route or a different page.
class ArtifactsController < ApplicationController
def index
#artifacts = Artifact.search(params[:search_text])
end
end
class Artifact < ActiveRecord::Base
def self.search(query)
if query
where('name ILIKE ?', "%#{query}%")
else
all
end
end
end
So then your form looks like:
<%= form_tag artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
Edit:
So what you really want to do is any page you want to search, include a form which makes a request to that same page.
Then in each of those controller methods just put this line of code:
#artifacts = Artifact.search(params[:search_text])
and that will populate the #artifcats array with only artifacts that match the search query.
Try using "Ransack" gem. It can also perform some more powerful searches.

Rails - Display a title only once in an .each block

Noob question here :)
I'm testing a variable, and if it exists, I'd like to display an .each loop with a title.
Of course, the title should be displayed only once. Is there a way to do it? Any best practice?
<%
#twitter_friends.each do |u|
if #user = User.is_a_member?(u.id)
%>
# HERE I'D LIKE TO DISPLAY THE TITLE ONLY AT FIRST ITERATION
<% #user.name %> is your twitter friend, and is a member.
<% end %>
<% end %>
Thanks !
I would normally recommend using each_with_index and checking for a zero index, but seeing as you have a conditional in the loop, you should use a check variable like so:
<% shown_title = false %>
<% #twitter_friends.each do |u| %>
<% if #user = User.is_a_member?(u.id) %>
# HERE I'D LIKE TO DISPLAY THE TITLE ONLY AT FIRST ITERATION
<% unless shown_title %>
<h1>My Title</h1>
<% shown_title = true %>
<% end %>
<% #user.name %> is your twitter friend, and is a member.
<% end %>
<% end %>