简体   繁体   中英

Routing issue with my controller action

I have a populate form method in my user controller.. the problem is when I fill in the text field in my view and click it wants to go trough show in my controller I don't have a show action in my user controller. any help would be greatly appreciated...

This is my errror

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):

This is my User controller

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

This is my view..

<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>

This is my 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);
      });
     }
   );
 });

This is my routes

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

Extra this is what I get when I rake my routes

              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

now I get this its not picking up what I have in my text box and it querying 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

It might be a bit confusing, but let's go through it together!

You have routes configured like this:

resources :user do
  collection do
    get :populate_form
  end
end

You've been expecting to visit some URL, to call proper action in your controller - populate_form , but for some reason it tries to route your request to show .

Let's check how your URL loos like:

/user/populate_form&emp_id=BILL

This causes the whole request to "fall" into route recognised as:

user GET /user/:id

which corresponds to show action. Whole string "populate_form&emp_id=BILL" falls into params[:id] in your show action. Why didn't it use proper action? We've been expecting to reach populate_form !

Try changing the URL you're visiting slightly from:

/user/populate_form&emp_id=BILL

to:

/user/populate_form?emp_id=BILL

This is easy change, but it should solve your problem!

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM