我是ruby on rails的新手。我试图重定向到outlet_controller#map,但下面的代码将我重定向到#show。我做错了什么。帮我整理一下。
my route is
namespace :api do
get 'outlet/map' => 'outlets#map'
end
我的控制器是
class Api::OutletsController < ApplicationController
http_basic_authenticate_with :name => "admin", :password => "password"
skip_before_filter :authenticate_user!
before_filter :fetch_outlets, :except => [:index, :create]
def fetch_outlets
@outlet = Outlet.find_by_id(params[:id])
end
def index
respond_to do |format|
@outlets = Outlet.select(:name, :description, :latitude, :longitude, :contact, :image_url, :emailid).all
#@outlets = Outlet.all
format.json { render json: @outlets}
end
end
def auto_complete
params.permit!
query = params[:query]
@outlet = Outlet.select(:name).where("name like ?","#{query}%")
if @outlet.present?
respond_to do |format|
format.json { render json: @outlet }
end
elsif
render :json=> {:status=>'error', :message=>'Data not found'}
end
end
def search
params.permit!
query = params[:query]
puts YAML::dump(query)
@outlet = Outlet.select(:name, :description, :latitude, :longitude, :contact, :image_url, :emailid).where("name like ?","%#{query}%")
if @outlet.present?
respond_to do |format|
format.json { render json: @outlet }
end
elsif
render :json=> {:status=>'error', :message=>'Data not found'}
end
end
def show
respond_to do |format|
format.json { render json: @outlet }
end
end
def create
@outlet = Outlet.new(params[:outlets])
respond_to do |format|
if @outlet
format.json { render json: @outlet, status: :created }
else
format.json { render json: @outlet.errors, status: :unprocessable_entity }
end
end
end
def update
if @outlet.update_attributes(outlet_params)
render :json=> {:status=>'success', :message=>'Successfully Updated'}
else
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
def map
super
end
def destroy
if @outlet.destroy
render :json=> {:status=>'success', :message=>'Successfully Removed'}
else
format.json { render json: @outlet.errors, status: :unprocessable_entity }
end
end
private
def outlet_params
params.require(:outlets).permit(:name, :brand_id, :latitude, :langitude, :location, :description, :image_url, :contact, :emailid)
end
end
我的开发日志
/api/outlets/map
Started GET "/api/outlets/map" for 127.0.0.1 at 2015-06-01 17:18:59 +0530
Processing by Api::OutletsController#show as JSON
Parameters: {"id"=>"map"}
[1m[35mOutlet Load (0.2ms)[0m SELECT `outlets`.* FROM `outlets` WHERE `outlets`.`id` = 0 LIMIT 1
Completed 200 OK in 15ms (Views: 0.3ms | ActiveRecord: 0.2ms)
为什么我重定向到outlets_controller#show?有谁能帮忙解决这个问题吗?
问题可能是routes.rb
中路由定义的顺序。
这个路由顺序将触发有问题的行为,因为Rails将使用匹配请求的第一个路由:
YourApp.routes.draw do
namespace :api do
resources :outlets # defines the show route
get 'outlet/map' => 'outlets#map'
end
end
要解决这个问题,改变路由的顺序:
YourApp.routes.draw do
namespace :api do
get 'outlet/map' => 'outlets#map'
resources :outlets # defines the show route
end
end
现在路由outlet/map
比outlet/:id
先匹配。
你也可以试试下面这个路由定义:
YourApp.routes.draw do
namespace :api do
resources :outlets do
get 'map', on: :collection
end
end
end