Rails路由 - 为路由添加控制器方法

时间:2015-01-13 15:55:28

标签: ruby-on-rails ruby-on-rails-3

使用Rails 3.2.18,我是新手,我正在尝试将此控制器添加到路由中。我不确定如何做到这一点,因为预先存在的格式与我读过的教程不同。我不知道在哪里添加控制器方法名称,因此/devices/deviceTextSearch指向device_text_search,视图可以通过device_text_search_path

引用它

路线片段:

  resources :devices, :only => [:index, :show, :update] do
    member do
      put :backup
      put :refresh
    end
    collection do
      get :find
    end

控制器代码段:

  # GET /devices/find
  def find
    device = current_domain.device.find_by_logical_name(params[:device_id].strip)

    respond_to do |format|
      if device.nil?
        flash[:notice] = "Device '#{params[:device_id]}' was not found"
        format.html { redirect_to devices_path }
      else
        format.html { redirect_to device_path(device) }
      end
    end
  end

  #GET /devices/deviceTextSearch
  def device_text_search
    if params[:q].nil?
      @device_text = []
    else
       @device_text = DeviceTextSearch.search params[:q]
    end
  end

1 个答案:

答案 0 :(得分:1)

尝试:

get '/devices/device_text_search' => 'devices#devise_text_search', as: 'devi    se_text_search'

这给出了以下路线:

                     devise_text_search GET    /devices/device_text_search(.:format)                                  devices#devise_text_search