控制器操作的路由问题

Routing issue with my controller action

本文关键字:问题 路由 操作 控制器      更新时间:2023-09-26

我的用户控制器中有一个填充表单方法。。问题是,当我在视图中填写文本字段并单击它时,我想在控制器中显示,但在用户控制器中没有显示操作。任何帮助都将不胜感激。。。

这是我的错误

Started GET "/user/populate_form&emp_id=BILL" for 127.0.0.1 at 2015-03-23 17:39:17 -0400
AbstractController::ActionNotFound (The action 'show' could not be found for UserController):

这是我的用户控制器

class UserController < ApplicationController

def populate_form
  @visual = Visual.find_by_id(params[:emp_id])
     render :json => {
        :emp_first_name => @emp_first_name
     }
  end
end
def show
  @visual = Visual.find_by_id(params[:emp_id])
     render :json => {
         :emp_first_name => @emp_first_name
     }
  end
end

这是我的看法。。

<div class='row form-group'>
  <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
   <%= f.text_field :emp_id, tabindex: 1, id: 'emp_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
  </div>
</div>
<div class='row form-group'>
  <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
   <%= f.text_field :emp_first_name, tabindex: 1, id: 'emp_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
  </div>
</div>

这是我的app.js

 $('#emp_id').change(function() {
      var url = '/users/populate_form/';
      $.getJSON(url, function(data) {
        if(!(data.emp_first_name === undefined))
        $('#emp_first_name').val(data.emp_first_name);
      });
     }
   );
 });

这是我的路线

Rails.application.routes.draw do

 devise_for :users, controllers: { sessions: 'sessions' }
 root to: 'entry#index'
 resources :entry do
 end
 resources :user do
    collection do
      get :populate_form
    end
  end
 # Routes for API calls only.
 namespace :api do
 end
end

额外的,这是我得到的,当我耙我的路线

              Prefix Verb     URI Pattern                    Controller#Action
            teaspoon          /teaspoon                      Teaspoon::Engine
    new_user_session GET      /users/sign_in(.:format)       sessions#new
        user_session POST     /users/sign_in(.:format)       sessions#create
destroy_user_session DELETE   /users/sign_out(.:format)      sessions#destroy
       user_password POST     /users/password(.:format)      devise/passwords#create
   new_user_password GET      /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET      /users/password/edit(.:format) devise/passwords#edit
                     PATCH    /users/password(.:format)      devise/passwords#update
                     PUT      /users/password(.:format)      devise/passwords#update
 cancel_user_registration GET      /users/cancel(.:format)        devise/registrations#cancel
  user_registration POST     /users(.:format)               devise/registrations#create
  new_user_registration GET      /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET      /users/edit(.:format)          devise/registrations#edit
                     PATCH    /users(.:format)               devise/registrations#update
                     PUT      /users(.:format)               devise/registrations#update
                     DELETE   /users(.:format)               devise/registrations#destroy
                root GET      /                              entry#index
          entry_index GET      /entry(.:format)               entry#index
                     POST     /entry(.:format)               entry#create
           new_entry GET      /entry/new(.:format)           entry#new
          edit_entry GET      /entry/:id/edit(.:format)      entry#edit
               entry GET      /entry/:id(.:format)           entry#show
                     PATCH    /entry/:id(.:format)           entry#update
                     PUT      /entry/:id(.:format)           entry#update
                     DELETE   /entry/:id(.:format)           entry#destroy
 populate_form_user_index GET      /user/populate_form(.:format)     user#populate_form
          user_index GET      /user(.:format)                user#index
                     POST     /user(.:format)                user#create
            new_user GET      /user/new(.:format)            user#new
           edit_user GET      /user/:id/edit(.:format)       user#edit
                user GET      /user/:id(.:format)            user#show
                     PATCH    /user/:id(.:format)            user#update
                     PUT      /user/:id(.:format)            user#update
                     DELETE   /user/:id(.:format)            user#destroy
                     GET|POST /entry(.:format)               entry#index

现在我得到了这个,它没有提取我文本框中的内容,它正在查询null。。。

Started GET "/user/populate_form&emp_id=BILL" for 127.0.0.1 at 2015-03-23 17:52:29 -0400
Processing by UserController#show as JSON
Parameters: {"id"=>"populate_form&emp_id=BILL"}
Visual Load (2.2ms)  SELECT  "EMPLOYEE".* FROM "EMPLOYEE"  WHERE "EMPLOYEE"."ID" IS NULL AND ROWNUM <= 1

这可能有点令人困惑,但让我们一起来看看吧!

您的路由配置如下:

resources :user do
  collection do
    get :populate_form
  end
end

您一直希望访问某个URL,以便在控制器populate_form中调用适当的操作,但由于某种原因,它试图将您的请求路由到show

让我们来检查一下你的URL是如何出现的:

/user/populate_form&emp_id=BILL

这导致整个请求"落入"被识别为的路线

user GET /user/:id

其对应于CCD_ 3动作。整个字符串"populate_form&emp_id=BILL"在show操作中属于params[:id]。为什么它没有采取适当的行动?我们一直期待着达到populate_form

尝试将您正在访问的URL从以下位置稍微更改:

/user/populate_form&emp_id=BILL

至:

/user/populate_form?emp_id=BILL

这是一个容易的改变,但它应该能解决你的问题!

希望能有所帮助!