我的控制器操作路由问题

时间:2015-03-24 13:08:50

标签: javascript ruby-on-rails ruby ruby-on-rails-4 routes

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

这是我的错误

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

1 个答案:

答案 0 :(得分:0)

这可能有点令人困惑,但让我们一起讨论它!

您的路由配置如下:

resources :user do
  collection do
    get :populate_form
  end
end

您一直希望访问某个网址,在您的控制器中调用正确的操作 - populate_form,但出于某种原因,它会尝试将您的请求发送到show

让我们检查一下您的网址如何:

/user/populate_form&emp_id=BILL

这会导致整个请求掉落&#34;进入路线认可:

user GET /user/:id

对应show动作。整个字符串&#34; populate_form&amp; emp_id = BILL&#34;在params[:id]行动中落入show。为什么没有采取适当的行动?我们一直希望能够达到populate_form

尝试更改您稍后访问的网址:

/user/populate_form&emp_id=BILL

为:

/user/populate_form?emp_id=BILL

这很容易改变,但它应该可以解决你的问题!

希望有所帮助!