传递除URL中的id之外的其他数据

时间:2016-12-09 21:11:26

标签: ruby-on-rails routing scaffolding

我目前正在创建一个允许访问者在线阅读杂志的网站。我为杂志本身创建了一个脚手架,另一个用于该杂志的页面。这样,我可以在创建杂志时为页面创建多重上传,并且所有内容都可以同时上传。

当我将页面资源嵌套在杂志的资源中时,如下所示:

resources :magazines do 
  resources :pages
end

我得到&#34; domain.com/magazines/<slug>/pages/id&#34;

但是,我对id有一些问题,因为它不能用于计算页面(因为id永远不会回到1),所以我在控制器内部创建了一个计算页面的函数对我来说,并使用&#34; page_number&#34;保存每个页面。值。

我的问题如下:如何,而不是id,我可以请求我的路由器使用:page_number? 我试图创建一个自定义路线,看起来像这样

resources :magazines do
  get '/:page_number' => 'pages#show', as: :custom_page
end

但由于一个未知的原因,Rails告诉我,我的自定义路由根本不存在,即使它在我输入rake routes时存在

提前谢谢

修改

这是我的 magazine_controller.rb

class MagazinesController < ApplicationController
  before_action :authenticate_user!, only: [:edit, :update, :destroy]
  before_action :set_magazine, only: [:show, :edit, :update, :destroy]

  def index
    @magazines = Magazine.all
  end

  def show
  end

  def new
    @magazine = Magazine.new
  end

  def edit
  end

  def create
    @magazine = Magazine.new(magazine_params)

    if @magazine.save
      if params[:images]
        (params[:image] || []).each_with_index do |image, index|
          @magazine.pages.create(image: image, page_number: index + 1)
      end
      redirect_to @magazine, notice: 'Magazine was successfully created.'
    else
      render :new
      end
    end
  end

  def update
    if @magazine.update(magazine_params)
      if params[:images]
        params[:images].each { |image|
          @magazine.pages.create(image: image)
        }
      end
      redirect_to @magazine, notice: 'Magazine was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @magazine.destroy
    redirect_to magazines_url, notice: 'Magazine was successfully destroyed.'
  end

  private

  def set_magazine
    @magazine = Magazine.friendly.find(params[:id])
  end

  def magazine_params
    params.require(:magazine).permit(:titre, :apercu)
  end

end

这里是 page_controller.rb

class PagesController < ApplicationController
  before_action :authenticate_user!, only: [:edit, :update, :destroy]
  before_action :set_page, only: [:show, :edit, :update, :destroy]

  def index
    @pages = Page.all
  end

  def show
  end

  def new
    @page = Page.new
  end

  def edit
  end

  def create
    @page = Page.new(page_params)

    if @page.save
      redirect_to @page, notice: 'Page was successfully created.'
    else
      render :new
    end
  end

  def update
    if @page.update(page_params)
      redirect_to @page, notice: 'Page was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @page.destroy
    redirect_to :back, notice: 'Page was successfully destroyed.'
  end

  private

  def set_page
    @page = Page.find(params[:id])
  end

  def page_params
    params.require(:page).permit(:titre, :apercu)
  end

end

修改2

以下是在尝试访问页面时来自development.log的输出

Started GET "/magazines/magazine-54/pages/11" for 127.0.0.1 at 2016-12-12 11:04:45 +0100
Processing by PagesController#show as HTML
  Parameters: {"magazine_id"=>"magazine-54", "id"=>"11"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)



NoMethodError (undefined method `pages' for nil:NilClass):

app/controllers/pages_controller.rb:49:in `set_page'
  Rendering /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.4ms)
  Rendering /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendering /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
  Rendered /var/lib/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (13.8ms)

1 个答案:

答案 0 :(得分:1)

使用这些路线

resources :magazines do 
  resources :pages
end

您应该能够更新PagesController并添加/更新以下方法:

def magazine
  @magazine = Magazine.find(params[:magazine_id])
end

def page
  @page = @magazine.pages.find_by(page_number: params[:id])
end

要使其有效,您应该page_number模型中的Page列和has_many :pages模型中的Magazine关联