Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms RubyonRails中的重发错误_Forms_Ruby On Rails 3 - Fatal编程技术网

Forms RubyonRails中的重发错误

Forms RubyonRails中的重发错误,forms,ruby-on-rails-3,Forms,Ruby On Rails 3,我是RubyonRails新手。我试图重定向到outlet#u controller#map,但下面的代码将我重定向到#show。我做错了什么。帮我整理一下 我的路线是 namespace :api do get 'outlet/map' => 'outlets#map' end 我的控制器是 class Api::OutletsController < ApplicationController http_basic_authenticate_with :name =&

我是RubyonRails新手。我试图重定向到outlet#u 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


为什么我被重定向到outlets#U 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
YourApp.routes.draw do
  namespace :api do
    get 'outlet/map' => 'outlets#map'
    resources :outlets # defines the show route
  end
end
YourApp.routes.draw do
  namespace :api do
    resources :outlets do
      get 'map', on: :collection
    end
  end
end