在轨道上的ruby中重写错误

时间:2015-06-01 11:59:56

标签: forms ruby-on-rails-3

我是铁杆上的红宝石新手。我尝试重定向到outlet_controller #map,但下面的代码将我重定向到#show。我做错了什么。帮我整理一下。

我的路线是

 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?任何人都可以帮忙解决这个问题......

1 个答案:

答案 0 :(得分:1)

问题可能是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/mapoutlet/:id之前匹配。

如果适用,您也可以尝试此路线定义:

YourApp.routes.draw do
  namespace :api do
    resources :outlets do
      get 'map', on: :collection
    end
  end
end