How do i access an id from the link? - ruby-on-rails

I am displaying list of cities from the database ,each as a link.I want when a user click on any of the cities ,the action respond by grabbng the id of that city and re use it on the next action(show action in this case).So How do you grab an id from the link?Below is my idea which didnt work.Thank you in advance.
View/cities
<%#cities.each do |city| %>
<%=city.name%><br/>
<%end%>
Controller
Class DealController < ApplicationController
def all_cities
#cities=City.find(:all)
end
def city
#city=City.find(params[:city_id])
session[:city_id] = #city.id
redirect_to :controller=>"deal",:action=>"show"
end
def show
unless session[:city_id].nil? || session[:city_id].blank?
#city = City.find(session[:city_id])
#deals=#city.deals
end
end
routes
get "deal/city"
match 'deal/city' => "deal#city"

You are not passing the city_id in the link. Try:
<% #cities.each do |city| %>
<%= city.name %><br/>
<% end %>
If you want the link to be like "/deal/city/1" and get 1 as params[:city_id], you can modify your routes like this:
match 'deal/city/:city_id' => "deal#city", :via => :get
and use the link like this:
<% #cities.each do |city| %>
<%= city.name %><br/>
<% end %>

Related

How to render search results only when the user press search

Does anyone know how to only display search results once you have clicked the search button?
At the moment, my page is currently displaying everything from my #flights.each. But I only want this information to become visible once they have clicked search :)
my index.html.erb
<h1>Flights#index</h1>
<p>Find me in app/views/flights/index.html.erb</p>
From Airport:
<%= form_for(flights_index_path, method: :get) do %>
<%= text_field_tag :from_airport_id, params[:from_airport_id] %>
<%= submit_tag 'Search' %>
<% end %>
<% #flights.each do |f| %>
<br>
<br>
Flight from <%= f.from_airport_id %>
Arriving at <%= f.to_airport_id %>
<% end %>
my controller
class FlightsController < ApplicationController
def index
if params[:from_airport_id]
#flights = Flight.where('from_airport_id LIKE ?', "%#{params[:from_airport_id]}%")
else
#flights = Flight.all
end
end
private
def flights_path
params.require(:flight).permit(:flight, :from_airport_id)
end
end
I believe it's because you have #flights = Flight.all in the else condition. When there is no query(such as after hitting the submit button and passing over the params) it will default to show all the flights. I'd take this line out and only have
def index
if params[:from_airport_id]
#flights = Flight.where('from_airport_id LIKE ?', "%#{params[:from_airport_id]}%")
else
#flights = []
end
end
Or you can look to have an AJAX request from your flights search and render the form that way.

How to pass an id in URL to controller? Rails

I want to deal with some data rows with same key_id. I want them to get select by using an id in URL, so I will have /field_data/index/12 and 12 here means the key_id.
I can manually do it in the controller by hardcoding the key_id
def index
#key_id = 12
#field_data = FieldDatum.select('field_content').where("key_id = ?", #key_id)
end
and my URL is very general: /field_data
So how to make it be able to get the id in URL?
routes
get "/field_data/index/:key_id", to: "field_datas#index"
controller
def index
#field_data = FieldDatum.select('field_content').where(key_id: params[:key_id])
end
Try this:
Show.html.erb
<% user_all.each do |user| %>
<%= link_to user_id_pass_path(user.id), method: :get do %>
<span class="btn btn-warning">Pass Id</span>
<% end %>
<% end %>
routes.rb
get '/user_id_pass/:id' => 'users#user_id_pass', as: 'user_id_pass'
users_controller.rb
def user_id_pass
#user = User.find_by_id(params[:id])
end

Controller not receiving object id

I am trying to pass the category.id to the shop/index controller using
<% Category.all.each do |category| %>
<div><%= link_to category.name.titleize, shop_index_path :category_id => category.id %></div>
<hr>
<% end %>
Controller:
class ShopController < ApplicationController
def index
#products = Category.find(params[:category_id]).products
end
end
Where a category has many products. But Whenever I click the link I get
Couldn't find Category without an ID
Can anyone see why this is?
EDIT: This is the route I use
get 'shop/index'
try add params:
shop_index_path(params: { category_id: category.id })
You need refine you routes for passing params:
get 'shop/index/:category_id', to: 'shop#index'
And now you can pass category_id to path helper like:
shop_index_path(category.id)
It should be
<%= link_to category.name.titleize, shop_index_path(category.id) %>
Surely you could just use:
shop_index_path(category.id)
And then load:
Category.find(params[:id]).products

Pulling records from one table (products) depending on a value in another (department)

I have a Products table and a Departments table.
In products model:
has_one :department
In departments model:
belongs_to :products
I also have a view in my products controller that lists all departments as so:
<% #departments.each do |department| %>
<div class="column_entry">
<%= link_to image_tag(department.image_attachment), products_of_a_given_main_cat_url %>
</div>
<% end %>
Those load up fine. When I click on one of the images I want products_of_a_given_main_category view to dynamically populate with products from the department whose image I clicked.
In my products controller:
def products_of_a_given_main_cat
#products = Product.all
#department = Department.where(:name == :department)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
and in the view:
<div class="the_grid_prod">
<% #products.each do |product| %>
<% if product.department == #department.name %>
<div class="column_entry">
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id) %>
</div>
<% end %>
<% end %>
</div>
When I click on the image for a department that I'm certain has products the page loads but there are no products. I'm certain I'm doing something very wrong but I'm not really sure what it is.
in routes file:
get 'products/products_of_a_given_main_cat' => :products_of_a_given_main_cat, :as => :products_of_a_given_main_cat
The problem is this:
#department = Department.where(:name == :department)
The :department is a symbol, not a string, and not a variable. Basically it is a number deep down, definietly not a name. Also, this will evaluate to Departments which have a name false. Don't do evaluation in the where part, only a hash.
You need the params[:department] if you get it from the request.
#department = Department.where(:name => params[:department])
Update:
Please modify your request link:
<%= link_to image_tag(department.image_attachment), products_of_a_given_main_cat_url(:department => department.name) %>
This will introduce a new parameter to the request, and it will work for you with the condition written above.
So to solve this problem I changed my associations as #TomL suggested in his comment. I also changed the following:
In products controller I deleted
#department = Department.where(:name => params[:department])
and changed
#products = Product.all
to
#products = Product.where(:department => params[:department])
The products_of_a_give_main_category_view now looks like this:
<div class="the_grid_prod">
<% #products.each do |product| %>
<div class="column_entry">
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id) %>
</div>
<% end %>
</div>
Thanks to both #Matzi and #TomL for their help. It's greatly appreciated.

Acts_as_taggable_on link_to

I installed the Acts as taggable on plugin to my 'Post' model, however I'm unable to call up a list of posts tagged with certain tag.
Here's what I have in show.
<%= link_to tag.name, posts_path(:view =>'tag', :tag => tag.name) %><% end %>
Except when I click on it, it shows all posts. I want it to show just the posts tagged with that keyword...what am I doing wrong here?
Thanks for your help
Modify your controller code so that it reads something like:
#posts = Post.tagged_with(params[:tag], :on => 'tags')
Here is what I ended up doing...
index view
<% job.tags.each do |tag| %>
<div class="tag"><%= link_to(tag, jobs_path(tag: tag.name), method: :get) %></div>
<% end %>
index action in the controller
if params[:tag]
#search_term = params[:tag]
#jobs = Job.tagged_with(#search_term)
end

Resources