Ruby on rails rubyonrails中的模型关系

Ruby on rails rubyonrails中的模型关系,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在使用active admin在我的表CustomPage和MenuItem中添加一行。 这是我的菜单模型 class MenuItem < ActiveRecord::Base belongs_to :custom_page after_create :set_url def set_url @menu_item = MenuItem.all url = "/pages/"+@menu_item.last.name.split(" ").join("_")

我正在使用active admin在我的表CustomPage和MenuItem中添加一行。 这是我的菜单模型

class MenuItem < ActiveRecord::Base
  belongs_to :custom_page
  after_create :set_url
  def set_url
    @menu_item = MenuItem.all
    url = "/pages/"+@menu_item.last.name.split(" ").join("_")
    @menu_item.last.update(url:url)
end
一个是我可以创建一个菜单项,并与创建的任何自定义页面链接。因此,当我点击一个菜单并查看相应的自定义页面时。通过这种方式,我可以创建任意数量的自定义页面,这些页面不会显示在前端

第二条路线是因为我想查看创建的自定义页面,这些页面没有与菜单项链接。我可以通过将自定义页面表中的url复制到浏览器中来查看页面

当我这样做的时候,我需要两个视图页面,两个控制器方法。。还有两条路

我有没有办法在单路线单方法单视图。。 到目前为止我已经试过了。。 在我的控制器中

class CustomPageController < ActionController::Base
  layout 'calculator'
  def load_content
    @menu_item = MenuItem.where(url: "/pages/"+params[:page_name])
    if @menu_item.first.custom_page.present?
      @description = @menu_item.first.custom_page.description
    else  
      @description = "CUSTOM PAGE NOT FOUND..."  
    end
  end

  def show_custom_page
    @custom_view = CustomPage.where(url: "/pages/view/"+params[:page])
    if @custom_view.first.present?
      @description = @custom_view.first.description      
    end
  end
end
def load_content
        @menu_item = MenuItem.where(url: "/pages/"+params[:page_name])
        @custom_page = CustomPage.where(url: "/pages/"+params[:page_name])
        if @menu_item.first.custom_page.present?
          @description = @menu_item.first.custom_page.description
        elsif @custom_page.first.present?
          @description = @custom_page.first.description
        else
          @description = "CUSTOM PAGE NOT FOUND..."  
        end
      end
我从控制器中删除了所有其他代码,并删除了第二条路线和第二个视图


但是当我试图通过url加载它时,在第
行if@menu\u item.first.custom\u page.present?
处出现了一个错误。。未定义自定义页面。。有什么方法可以做到这一点……或者我应该坚持我的老方法吗?
@custom\u page=CustomPage.all
只需获得
@custom\u page.last
是一个惊人的性能杀手,除非你使用的是Rails 4
get '/pages/:page_name' => 'custom_page#load_content'
get '/pages/view/:page' => 'custom_page#show_custom_page'
def load_content
        @menu_item = MenuItem.where(url: "/pages/"+params[:page_name])
        @custom_page = CustomPage.where(url: "/pages/"+params[:page_name])
        if @menu_item.first.custom_page.present?
          @description = @menu_item.first.custom_page.description
        elsif @custom_page.first.present?
          @description = @custom_page.first.description
        else
          @description = "CUSTOM PAGE NOT FOUND..."  
        end
      end