【问题标题】:Routing issue with my controller action我的控制器操作的路由问题
【发布时间】:2015-03-24 13:08:49
【问题描述】:

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

这是我的错误

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

【问题讨论】:

  • 根据错误,你没有在UsersController中定义show动作,你能检查一下你有没有,还是拼写错误?从您显示的代码中它丢失了
  • 更新了用户控制器.. 但现在我收到一个新错误,位于 rake 路由框下方... @Paweł Dawczak
  • 嗯...在“rake routes下方的框”中没有错误...您能提供吗?
  • 好吧,这并不是一个真正的错误,它只是返回 null--------...开始 GET "/user/populate_form&emp_id=BILL" for 127.0.0.1 at 2015-03-23 17:52 :29 -0400 UserController 处理#show as JSON 参数:{"id"=>"populate_form&emp_id=BILL"} 视觉负载 (2.2ms) SELECT "EMPLOYEE".* FROM "EMPLOYEE" WHERE "EMPLOYEE"."ID" IS NULL AND ROWNUM

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


【解决方案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

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

尝试稍微更改您访问的 URL:

/user/populate_form&emp_id=BILL

到:

/user/populate_form?emp_id=BILL

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

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多