1
votes

I tried to route as following with my 2 models pimp and mepager (one-to-one relation):

/config/routes.rb

root :to => 'pimps#index'
get '/:pimp_id/mepager', :to => 'mepager#show', :as => 'mepager'

My mepager show action looks like this:

/app/controllers/mepagers_controller.rb

before_action :set_pimp    

def show
    @mepager = @pimp.mepager
end

private
def set_pimp
    @pimp = Pimp.find(params[:pimp_id])
end

Now I tested in the rails console the function Pimp.first.mepager what should be equal to @pimp.mepager and it gives me the right mepager object back with all atttributes. But if I try to open '/:pimp_id/mepager' in my webapplication (with an existing pimp that has a mepager linked to it) I get following error

Routing Error
  uninitialized constant MepagerController

Did I miss something?

Regards.

Complete error msg:

Started GET "/1/mepager" for 127.0.0.1 at 2014-06-06 08:55:20 +0200

ActionController::RoutingError (uninitialized constant MepagerController): activesupport (4.0.0) lib/active_support/inflector/methods.rb:226:in const_get'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:226:in
block in constantize' activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in each' activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:ininject'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in constantize' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:76:incontroller_reference' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:66:in controller' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:44:incall' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in
each' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in call' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in call' rack (1.5.2) lib/rack/etag.rb:23:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:incall' rack (1.5.2) lib/rack/head.rb:11:in call' actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:incall'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:incontext' rack (1.5.2) lib/rack/session/abstract/id.rb:220:in call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in
call' activerecord (4.0.0) lib/active_record/query_cache.rb:36:in call' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in cal l' activerecord (4.0.0) lib/active_record/migration.rb:369:in call' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in _run__226962734__call__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:inrun_callbacks' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in
call' actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in call' actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call' actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in
call_app' railties (4.0.0) lib/rails/rack/logger.rb:21:in block in call' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:inblock in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:intagged' railties (4.0.0) lib/rails/rack/logger.rb:21:in call' actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:incall' rack (1.5.2) lib/rack/methodoverride.rb:21:in call' rack (1.5.2) lib/rack/runtime.rb:17:incall' activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in call' rack (1.5.2) lib/rack/lock.rb:17:incall' actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in call' railties (4.0.0) lib/rails/engine.rb:511:incall' railties (4.0.0) lib/rails/application.rb:97:in call' rack (1.5.2) lib/rack/lock.rb:17:incall' rack (1.5.2) lib/rack/content_length.rb:14:in call' rack (1.5.2) lib/rack/handler/webrick.rb:60:inservice'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in
run'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch /middleware/templates/rescues/_trace.erb (1.0ms) Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch /middleware/templates/routes/_route.html.erb (1.0ms) Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch /middleware/templates/routes/_table.html.erb (1.0ms) Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch /middleware/templates/rescues/routing_error.erb within rescues/layout (42.0ms)

Console test

> irb(main):001:0> Pimp.first.mepager   ←[1m←[36mSQL (0.0ms)←[0m 
> ←[1mUSE [rails_confreport_develop]←[0m   ←[1m←[35mPimp Load
> (1.0ms)←[0m  EXEC sp_executesql N'SELECT TOP (1) [pimps].* Y
> [pimps].[id] ASC'   ←[1m←[36mMepager Load (1.0ms)←[0m  ←[1mEXEC
> sp_executesql N'SELECT TOP (1) [me gers] WHERE [mepagers].[pimp_id] =
> @0 ORDER BY [mepagers].[id] ASC', N'@0 int', id", 2]]
> => #<Mepager id: 2, pimp_id: 2, pre: "geqgewqrgq", post: "egewqewqgewqgewq", com .0, save_c: nil, save_other: "", save_otherv:
> nil, affect_design: "", affect_str "", affect_other: "", affect_dno:
> "", affect_mid: "", affect_otherdoc: "", owner ence: "", created_at:
> "2014-06-06 06:36:44", updated_at: "2014-06-06 06:36:44">

mepagers_controller.rb

class MepagersController < ApplicationController
  before_action :set_mepager, only: [:show, :edit, :update, :destroy]
  before_action :set_pimp

  # GET /mepagers
  # GET /mepagers.json
  #def index
    #@mepagers = Mepager.all
  #end

  # GET /mepagers/1
  # GET /mepagers/1.json
  def show
    @mepager = @pimp.mepager
  end

  # GET /mepagers/new
  def new
    @mepager = @pimp.build_mepager
  end

  # GET /mepagers/1/edit
  def edit
    @mepager = @pimp.mepager
  end

  # POST /mepagers
  # POST /mepagers.json
  def create
    @mepager = @pimp.build_mepager(mepager_params)

    respond_to do |format|
      if @mepager.save
        format.html { redirect_to [@pimp,@mepager], notice: 'Mepager was successfully created.' }
        format.json { render action: 'show', status: :created, location: @mepager }
      else
        format.html { render action: 'new' }
        format.json { render json: @mepager.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /mepagers/1
  # PATCH/PUT /mepagers/1.json
  def update
    @mepager = @pimp.mepager
    respond_to do |format|
      if @mepager.update(mepager_params)
        format.html { redirect_to [@pimp,@mepager], notice: 'Mepager was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @mepager.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /mepagers/1
  # DELETE /mepagers/1.json
  def destroy
    @mepager = @pimp.mepager
    @mepager.destroy
    respond_to do |format|
      format.html { redirect_to mepagers_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_mepager
      @mepager = Mepager.find(params[:id])
    end

    def set_pimp
      @pimp = Pimp.find(params[:pimp_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def mepager_params
      params.require(:mepager).permit(:pimp_id, :pre, :post, :comment, :save_h, :save_c, :save_other, :save_otherv, :affect_design, :affect_stress, :affect_me, :affect_other, :affect_dno, :affect_mid, :affect_otherdoc, :owner, :pps, :reference)
    end
end
2
What is your Controller classname? It should be MepagersController as the file name is mepagers_controller.rbPavan
Can you post the line at which it is showing the error?Pavan
I posted the whole error msg. As you can see its not giving me a specific line.Syk
Can you post your mepagers_controller.rb with classname?Pavan
Done. Sry for bad format dunno whats the problem here.Syk

2 Answers

0
votes

You have written get '/:pimp_id/mepager', :to => 'mepager#show', :as => 'mepager' but it should be get '/:pimp_id/mepager', :to => 'mepagers#show', :as => 'mepager'. You forgot to add "s" in your "mepages#show". For more information refer to here.

One more thing since your mepager is nested inside pimp you don't need your :set_mepager filter. You can either remove it or do something like this inside your mepagers_controller.rb

class MepagersController < ApplicationController
  before_action :set_pimp
  before_action :set_mepager, only: [:show, :edit, :update, :destroy]
  # your actions

  private
   # Use callbacks to share common setup or constraints between actions.
   def set_mepager
    @mepager = @pimp.mepager
   end

   def set_pimp
    @pimp = Pimp.find(params[:pimp_id])
   end
end
0
votes

You need to modify your routes this way:

root :to => 'pimps#index'
get '/:pimp_id/mepager', :to => 'mepagers#show', :as => 'mepager'

Rails infers the controller name from the :to keyword. The notation mepagers#show means the show method of MepagersController

Source: http://edgeguides.rubyonrails.org/routing.html#connecting-urls-to-code