路由问题一对一关联

时间:2014-06-06 06:51:03

标签: ruby-on-rails routing associations one-to-one

我尝试使用我的2个模型pimp和mepager(一对一关系)路由如下:

/config/routes.rb

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

我的mepager show动作如下:

/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

现在我在rails控制台中测试了函数Pimp.first.mepager什么应该等于@ pimp.mepager它给了我正确的mepager对象返回所有atttributes。但如果我试图打开' /:pimp_id / mepager'在我的webapplication中(现有的pimp与mepager相关联)我得到以下错误

Routing Error
  uninitialized constant MepagerController

我错过了什么吗?

问候。

完成错误消息:

  

开始GET" / 1 / mepager" for 2014.0-06 08:55:20 +0200

的127.0.0.1      

ActionController :: RoutingError(未初始化的常量   MepagerController):activesupport(4.0.0)   lib / active_support / inflector / methods.rb:226:在const_get'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:226:in
块中进行constantize' activesupport(4.0.0)   lib / active_support / inflector / methods.rb:224:在each' activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in注入'
  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:in controller_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:in呼叫' ActionPack的   (4.0.0)lib / action_dispatch / journey / router.rb:71:block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in
每个'   actionpack(4.0.0)lib / action_dispatch / journey / router.rb:59:在call' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in 来电' rack(1.5.2)lib / rack / etag.rb:23:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:in call'机架(1.5.2)   lib / rack / head.rb:11:in call' actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in来电'
  actionpack(4.0.0)lib / action_dispatch / middleware / flash.rb:241:in   call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:in上下文'   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:in阻止通话'
  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:in run_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 来电' 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:在标记的block in call' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in块中'
  activesupport(4.0.0)lib / active_support / tagged_logging.rb:25:in   tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in标记'铁路(4.0.0)   lib / rails / rack / logger.rb:21:in call' actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in来电'架   (1.5.2)lib / rack / methodoverride.rb:21:在call' rack (1.5.2) lib/rack/runtime.rb:17:in电话' activesupport(4.0.0)   lib / active_support / cache / strategy / local_cache.rb:83:in call' rack (1.5.2) lib/rack/lock.rb:17:in call' actionpack(4.0.0)   lib / action_dispatch / middleware / static.rb:64:in call' railties (4.0.0) lib/rails/engine.rb:511:in call'铁路(4.0.0)   lib / rails / application.rb:97:in call' rack (1.5.2) lib/rack/lock.rb:17:in call'机架(1.5.2)   lib / rack / content_length.rb:14:in call' rack (1.5.2) lib/rack/handler/webrick.rb:60:in service'
  C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:在   service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in
运行'
  C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:在   `阻止在start_thread'

     

渲染   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)已呈现   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)已呈现   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)已呈现   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   (42.0ms)

控制台测试

> 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 个答案:

答案 0 :(得分:0)

你写过 get'/:pimp_id / mepager',:to =&gt; 'mepager#show',:as =&gt; 'mepager'但它应该是'/:pimp_id / mepager',:to =&gt; 'mepagers#show',:as =&gt; 'mepager'即可。你忘了在你的“mepages#show”中添加“s”。有关更多信息,请参阅here

由于你的mepager嵌套在pimp中,你不需要你的:set_mepager过滤器。您可以在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

答案 1 :(得分:0)

您需要以这种方式修改路线:

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

Rails从:to关键字推断出控制器名称。表示法mepagers#show表示show

MepagersController方法

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