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
Related
I'm trying to figure out how to get the store id on the controller side to delete the store.
Currently with this code my params on the controller side sends the store.id(1) as the value to the key format. I need to retrieve this by store_id: instead.
{..."controller"=>"home", "action"=>"delete_store", "format"=>"1"}
What I need:
{..."controller"=>"home", "action"=>"delete_store", "store_id"=>"1"}
HTML/ERB:
<h4>Your Stores:</h4>
<% #my_stores.each do |store| %>
<p><%= store.name %><%= link_to "X", delete_store_path(store.id), method: :delete %></p>
<% end %>
Controller:
class HomeController < ApplicationController
...
def delete_store
# With current code, I would have to do
# current_user.stores.where(store_id: params[:format] )
# But to make it proper I need this
# current_user.stores.where(store_id: params[:store_id] )
end
end
You can get it in the params with a hidden_field_tag:
<%= hidden_field_tag :store_id, store.id %>
This should allow you to do a lookup like:
params[:store_id]
Here is a link to the apidock for those wanting more info on hidden_field_tag.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag
In my form I want to iterate over a specific collection and collect the same information about each. For simplicity's sake something like:
<%= form_tag :update_dog do %>
<% #dogs.each do |dog| %>
<%= text_field_tag :name, :class=>dog.id %>
<% end %>
<%= submit_tag "Add", :class => 'btn btn-success'%>
<%= end %>
Where I would want to collect each dog's name to manipulate in the controller (in which I'd like to be able to iterate over each submitted dog name and access its id). The potential number of dogs in my collection is variable. What is the best way to do this? The code above is what I have so far but I have no idea if it's right and if so, how to use it in the controller.
Thanks so much!
I'd start with a filter. Create a before_filter that creates your dogs
class KennelController < ApplicationController
before_filter :get_dogs , :only=>[:new,:edit]
def get_dogs
#dogs = Dog.all.map{|d| [d.name, d.id]}
end
....
end
Then in either your new or edit views, you could do this:
<%= select_tag :dog, options_for_select(#dogs) %>
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 %>
I'm using rails 3 and am trying to use the in_place_editing plugin:
http://github.com/wanglian/in_place_editing
# Controller
class BlogController < ApplicationController
in_place_edit_for :post, :title
end
# View
<%= in_place_editor_field :post, 'title' %>
However I'm getting the error: id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
I'm calling the plugin in my photo_album controller, which has a title attribute...
class PhotoAlbumsController < ApplicationController
in_place_edit_for :photo_album, :title
The in the Index View, I'm doing the following:
<% #photoalbums.each do |photoalbum| %>
<%= in_place_editor_field :photoalbum, 'title' %>
<% end %>
Does anyone understand this or have experience with this plugin?
Thanks
The error is because, its trying to update the title of a nil object. You should use this instead
<% #photoalbums.each do |photoalbum| %>
<%= in_place_editor_field photoalbum, 'title' %>
<% end %>
if you see the code of the plugin the definition of the method is
def in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {})
In case you need to use in_place_editor_field in a loop, do something like this:
Class Item < AR::Base
#attributes like name, price
end
<% for some_item in #items %>
<% #item = some_item %>
<%= in_place_editor_field :item, :price %>
<% end %>
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