I have a model which is linking to the show method:
<%= link_to "View Others", :controller => "browse",
:action => "show", :id => #id, :d => "25" %>
Clicking the link gives:
http://localhost:3000/browse/santa-cruz?d=25
Rails gives the error though:
No route matches {:controller=>"browse", :action=>"show", :id=>nil, :d=>"25"}
If I take the extra parameter off everything works.
<%= link_to "View Others", :controller => "browse",
:action => "show", :id => #id %>
Goes to http://localhost:3000/browse/santa-cruz
This page loads and I am getting the correct params[:id]
Any ideas?
Pasted bellow is the show for my controller
def show
if params[:d].nil? then
# Show list of addresses in city.
addresses = Address.where(:slug => params[:id])
profile = []
addresses.each do |ad|
profile << ad.profile
end
unless profile.blank?
#profile = Kaminari.paginate_array(profile).page(params[:page]).per(5)
#title = "Profiles Near " + addresses.first.city
#id = params[:id]
else
redirect_to :controller => 'pages', :action => 'notlaunched', :zip => params[:id]
end
else # :d exists
# show all within :d miles.
addresses = Address.where(:slug => params[:id])
nearby = Address.near("#{addresses.first.fulladdress}", params[:d], :order => :distance)
profiles = nearby.map{ |ad| ad.profile }.uniq
end
end
Here's the Index:
def index
cities = []
states = []
Address.find_each do |ad|
cities << { :city => ad.city, :slug => ad.slug } # slug becomes the :id of show
states << ad.state
end
#cities = cities.uniq
#states = states.uniq
#title = "Browse Cities"
end
Related
offers_controller.rb file
def store_locator
city_name = params[:city]
#flag = true
per_page = 5
if !city_name.blank?
store = StoreCity.find_by_city(city_name.downcase)
end
if !store.nil?
#storelist= StoreAddress.where(:city_id=>store.id)
#flag = false
else
#storelist= StoreAddress.order(:store_name).paginate(:page => params[:page], :per_page => per_page)
end
respond_to do |format|
format.html
end
end
store_locator.html.erb pops up following error
No route matches {:action=>"store_locator", :controller=>"spree/offers", :escape=>nil, :page=>2}** at
<%=will_paginate #storelist ,:params => { :escape => nil }, :class => 'store_paging'%>
code snippet:
<div class="col-md-12" id="loadicon"></div>
<%if #flag%>
<%=will_paginate #storelist ,:params => { :escape => nil }, :class => 'store_paging'%>
<%end%>
<div id="storediv" style="background-color:#efefef;">
routes.rb has proper namespacing
get '/store_locator' => 'offers#store_locator'
will_paginate version 3.1.5
RoR version 4.2.3
check routes using command:
rake routes
which list all routes and check /store_locator is call which controller and action.
all,
I tried this:
<h9>Thursday</h9>
<% #thursday.each do |chorelist| %><br>
<p2><%= chorelist.name %></p2>
<%= button_to "delete", chorelists_destroy_path(:id, chorelist.id) %>
<% end %>
It won't do the trick, however.
As you can see, I am trying to add a delete button with each iteration. What am I missing here?
I'm still new to Rails, so any help will be most appreciated.
Addendum: I received this error message when using that latter method:
"Couldn't find Chorelist with 'id'={:name=>"Sweep Floors", :day=>"Sunday", :user_id=>2}"
I checked the console, and those parameters are all there.
class SaveListController < ApplicationController
before_filter :authenticate_user!
def index
#chorelist = Chorelist.create(user_id: params[:user_id], chore_id: params[:chore_id], day: params[:day], name: params[:chore_name])
redirect_to pick_chores_path
end
def display
#chorelist = Chorelist.all
#monday = #chorelist.where("day = 'Monday'", user_id: current_user.id)
#tuesday = #chorelist.where("day = 'Tuesday'", user_id: current_user.id)
#wednesday = #chorelist.where("day = 'Wednesday'", user_id: current_user.id)
#thursday = #chorelist.where("day = 'Thursday'", user_id: current_user.id)
#friday = #chorelist.where("day = 'Friday'", user_id: current_user.id)
#saturday = #chorelist.where("day = 'Saturday'", user_id: current_user.id)
#sunday = #chorelist.where("day = 'Sunday'", user_id: current_user.id)
end
def destroy
#chorelist = Chorelist.find(id: params[:id])
#chorelist.destroy
redirect_to show_chores_path
end
end
Rails.application.routes.draw do
resources :chorelists
get 'about' => 'welcome#about'
get 'contact' => 'welcome#contact'
root 'welcome#index'
get 'about' => 'welcome#about'
get 'pick_chores' => 'pick_chores#index'
post 'save_list' => 'save_list#index'
get 'show_chores' => 'save_list#display'
post 'chorelists_destroy' => 'save_list#destroy'
resources :chores
devise_for :users
end
Change :id, chorelist.id to id: chorelist.id
<h9>Thursday</h9>
<% #thursday.each do |chorelist| %><br>
<p2><%= chorelist.name %></p2>
<%= button_to "delete", chorelists_destroy_path(id: chorelist.id) %>
<% end %>
On controller
def destroy
#chorelist = Chorelist.find(params[:id])
#chorelist.destroy
# do your things
end
I don't understand why I'm getting this error message when I run RSpec:
Failure/Error: post :create
ActionController::RoutingError:
No route matches {:controller=>"stocks", :action=>"create"}
The controller stocks exists, the action create exists, and the route it should be using is this:
match 'stocks/:user_id' => 'stocks#create', :via => :post, :as => :query
Route File:
FruthScreener::Application.routes.draw do
root :to => 'stocks#index' # add user_id
match 'stocks/:user_id' => 'stocks#create', :via => :post, :as => :query
match 'stocks/:user_id' => 'stocks#destroy', :via => :delete, :as => :reset
match 'stocks/:user_id/update' => "stocks#update_index", :via => :post
Stock Controller: (Yes, this will be DRYed up)
class StocksController < ApplicationController
def index
#user = User.create
#user_id_num = #user.id
#user.stocks = Stock.all
#user.save
selected_user_stocks = (UserStock.where("user_id = #{#user_id_num} AND selected = true")).take(25)
#stocks = UserStock.convert_to_stocks(selected_user_stocks)
#pages = Pagination.determine_num_pages(#user.stocks)
end
def update_index
#user_id_num = params[:user_id]
#user = User.find(#user_id_num)
selected_user_stocks = UserStock.where("user_id = #{#user_id_num} AND selected = true")
#stocks = UserStock.convert_to_stocks(selected_user_stocks)
#pages = Pagination.determine_num_pages(#stocks)
#stocks = Stock.display(#stocks, params[:pageNumber].to_i)
render partial: 'stock_data'
end
def create
UserStock.eliminate_stocks(params) # thinking it's like "creating" a new batch of stocks
#user_id_num = params[:user_id]
#user = User.find(#user_id_num)
selected_user_stocks = UserStock.where("user_id = #{#user_id_num} AND selected = true")
#stocks = UserStock.convert_to_stocks(selected_user_stocks)
#pages = Pagination.determine_num_pages(#stocks)
#stocks = Stock.display(#stocks, 1)
render partial: 'stock_data'
end
def destroy
stocks_to_reset = UserStock.where("user_id = #{params[:user_id]}")
stocks_to_reset.each do |user_stock|
user_stock.selected = true
user_stock.save
end
#user_id_num = params[:user_id]
#user = User.find(#user_id_num)
selected_user_stocks = UserStock.where("user_id = #{#user_id_num} AND selected = true")
#stocks = UserStock.convert_to_stocks(selected_user_stocks)
#pages = Pagination.determine_num_pages(#stocks)
#stocks = Stock.display(#stocks, 1)
render partial: 'stock_data'
end
end
Stock Controller Spec: (test at bottom is failing)
require 'spec_helper'
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
describe StocksController do
context "index" do
before do
#user_count = User.all.count
#stock_count = Stock.all.count
get :index
end
it "should create a new user" do
expect(User.all.count).to eq(#user_count + 1)
end
it "should associate all stocks with the user" do
expect(User.last.stocks.count).to eq(#stock_count)
end
end
context "create" do # investigate why this isn't working...
it "should render the stock_list partial" do
user = User.create
post :create
response.should render_template(partial: "stock_list")
end
end
end
Thanks!!
match 'stocks/:user_id' => 'stocks#create', :via => :post, :as => :query
tells us that it needs :user_id to be a valid route.
post :create
has no :user_id... so it doesn't refer to a valid route.
I'd change it to
post :create, :user_id => user.id
post :create, params: {user_id: user.id}
How to redirect the two links to two urls by using single method in controller?
Example:
def index
#location_id = Location.find(#location_id)
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
if # PDF EPC link clicked
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end
if # LIVE EPC link clicked
#epc = Enr::Rds::XmlData.find_by_location_id(#location_id)
redirect_to #epc.report_url
end
end
in my view,
<%= link_to 'PDF', enr_location_current_epc_index_path(#location) %>
<%= link_to 'LIVE', enr_location_current_epc_index_path(#location) %>
in my routes
resources :current_epc, only: [:index, :show] do
get :download, :on => :collection
end
I would consider create 2 differente actions. One for each case. It would make your actions and your code much easier to read.
Then u would result with 3 actions. Index which would only load the initial objects, one to treat the specific id by the first logic and another link to treat the second logic.
def index
#location_id = Location.find(#location_id)
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
end
pdf_epc
#location_id = Location.find(#location_id)
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end
def live_epc
#epc = Enr::Rds::XmlData.find_by_location_id(#location_id)
redirect_to #epc.report_url
end
in your routes
resources :current_epc, only: [:index, :show] do
get :download, :on => :collection
end
get "/pdf_epc/:id" => "current_epc#pdf_epc", :as => enr_location_current_epc_pdf
get "/live_epc/:id" => "current_epc#live_epc", :as => enr_location_current_live_epc
in your view
<%= link_to 'PDF', enr_location_current_epc_pdf_path(#location) %>
<%= link_to 'LIVE', enr_location_current_live_epc_path(#location) %>
def pdf_url
if (params[:url] == 1)
do something
else
do something else
end
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
if #epc != nil
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end
end
In your routes.rb:
match "/anything/pdf_url/:url" => "anything#pdf_url"
And your two links:
<%= link_to "first", "/anything/pdf_url/1" %>
<%= link_to "second", "/anything/pdf_url/2" %>
EDIT:
member is used when it requires an :id parameter, if not it is a collection. Anyway, I would use match in that case like (which is in parenthesis is optional):
match "/anything(/download/:url)" => "anything#index"
and get the parameter in your controller like this:
def index
if params[:url] == 1 # Or whatever you put in your link_to
# redirect_to url
else
# redirect_to url
end
end
EDIT 2:
Index controller:
def index
if params[:id]
#location_id = Location.find(params[:id])
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
if params[:url] == 'pdf'
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
elsif params[:url] == 'live'
#epc = Enr::Rds::XmlData.find_by_location_id(#location_id)
redirect_to #epc.report_url
end
else
#locations = Location.all
respond_to do |format|
format.html
format.json { render :json => #locations }
end
end
end
Your routes:
match "/anything(/:id(/:url))" => "anything#index"
Your view (change links to fit your tastes, it's just a simple example):
<%= link_to "first", "/anything/1/pdf" %>
<%= link_to "second", "/anything/1/live" %>
I have an index view that lists all of the tags for my Entry and Message models. I would like to only show the tags for Entries in this view. I'm using acts-as-taggable-on.
Tags Controller:
def index
#letter = params[:letter].blank? ? 'a' : params[:letter]
#tagged_entries = Tagging.find_all_by_taggable_type('Entry').map(&:taggable)
#title = "Tags"
if params[:letter] == '#'
#data = Tag.find(#tagged_entries, :conditions => ["name REGEXP ?",
"^[^a-z]"], :order => 'name', :select => "id, name")
else
#data = Tag.find(#tagged_entries, :conditions => ["name LIKE ?",
"#{params[:letter]}%"], :order => 'name', :select => "id, name")
end
respond_to do |format|
flash[:notice] = 'We are currently in Beta. You may experience errors.'
format.html
end
end
tags#index:
<% #data.each do |t| %>
<div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<% end %>
I want to show only the taggable type 'Entry' in the view.
Any ideas? Thank you for reading my question.
SECOND EDIT:
Tags Controller:
def index
#title = "Tags"
#letter = params[:letter].blank? ? 'a' : params[:letter]
#taggings = Tagging.find_all_by_taggable_type('Entry', :include => [:tag, :taggable])
#tags = #taggings.map(&:tag).sort_by(&:name).uniq
#tagged_entries = #taggings.map(&:taggable)#.sort_by(&:id)#or whatever
if params[:letter] == '#'
#data = Tag.find(#tags, :conditions => ["name REGEXP ?",
"^[^a-z]"], :order => 'name', :select => "id, name")
else
#data = Tag.find(#tags, :conditions => ["name LIKE ?",
"#{params[:letter]}%"], :order => 'name', :select => "id, name")
end
respond_to do |format|
format.html
end
end
tags#index:
<% #data.each do |t| %>
<div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<% end %>
Max Williams' code works except when I click on my alphabetical pagination links. The error I'm getting [after I clicked on the G link of the alphabetical pagination] reads:
Couldn't find all Tags with IDs (77,130,115,...) AND (name LIKE 'G%') (found 9 results, but was looking for 129)
Can anyone point me in the right direction?
#taggings = Tagging.find_all_by_taggable_type('Entry', :include => [:tag, :taggable])
#tags = #taggings.map(&:tag).sort_by(&:name)
#tagged_entries = #taggings.map(&:taggable).sort_by(&:id)#or whatever