首页 文章

路由问题一对一关联

提问于
浏览
1

我尝试使用我的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 . 但是,如果我尝试在我的webapplication中打开'/:pimp_id / mepager'(现有的pimp与mepager相关联)我得到以下错误

Routing Error
  uninitialized constant MepagerController

我错过了什么?

问候 .

完整错误消息:

开始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:在const_get'activesupport(4.0.0)lib / active_support / inflector / methods.rb:226:在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:在调用块'actionpack(4.0.0)lib / action_dispatch / journey / router.rb:59:ineach'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:在调用'rack(1.5.2)lib / rack / etag.rb:23:在call'rack(1.5.2)lib / rack / conditionalget.rb:25:incall' rack(1.5.2)lib / rack / head.rb:11:在调用'actionpack(4.0.0)lib / action_dispatch / middleware / params_parser.rb:27:incall'actionpack(4.0 .0)lib / action_dispatch / middleware / flash.rb:241:在调用'rack(1.5.2)lib / rack / session / abstract / id.rb:225:incontext' rack(1.5.2)lib / rack / session / abstract / id.rb:220:在调用'actionpack(4.0.0)lib / action_dispatch / middleware / cookies.rb:486:在调用'activerecord(4.0.0)lib / active_record / query_cache.rb:36:在调用'activerecord(4.0.0)lib / active_record / connection_adapters / abstract / connection_pool.rb:626:在cal l'activerecord(4.0.0)lib / active_record / migration.rb:369:在调用'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:在调用'actionpack(4.0.0)lib / action_dispatch / middleware / debug_exceptions.rb:17:在调用'actionpack(4.0.0)lib中/action_dispatch/middleware/show_exceptions.rb:30:调用'railties(4.0.0)lib / rails / rack / logger.rb:38:incall_app'railties(4.0.0)lib / rails / rack / logger.rb: 21:在调用块中'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:凹进'railties(4.0.0)lib / rails / rack / logger.rb:21:在调用'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'activesu pport(4.0.0)lib / active_support / cache / strategy / local_cache.rb:83:在调用'rack(1.5.2)lib / rack / lock.rb:17:incall'actionpack(4.0.0)lib / action_dispatch /middleware/static.rb:64:in调用'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运行'C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:在`block in 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

    你写了 get '/:pimp_id/mepager', :to => 'mepager#show', :as => 'mepager' 但应该得到 '/:pimp_id/mepager', :to => 'mepagers#show', :as => '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
    
  • 0

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

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

    Rails从 :to 关键字推断出控制器名称 . 符号 mepagers#show 表示 MepagersControllershow 方法

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

相关问题