无法从我的模型

时间:2015-10-23 21:16:16

标签: ruby-on-rails ruby ruby-on-rails-4 model scope

我是ROR的新手并且正在学习使用教程。在为我的博客创建cms时,我遇到了这个错误:未定义的方法`id'为零:NilClass。 我试图将页面嵌套到我的主题上,这样当我使用主题查看页面时,我只看到具有该主题ID的特定页面。 所以,首先我在主题页面上创建了一个视图页面链接。主题视图的代码:

<% @page_title = "Subjects"%>
<%= link_to("Back to menu", {:controller => 'access', :action => 'index'}, :class => 'action new') %>

<div class="subjects index">
  <h2>Subjects</h2>

  <%= link_to("Add New Subject", {:action => 'new'}, :class => 'action new') %>

  <div><%= pluralize(@subjects.size, 'subject')%> found</div>
  <table class="listing" summary="Subject list">
    <tr class="header">
      <th>&nbsp;</th>
      <th>Subject</th>
      <th>Visible</th>
      <th>Pages</th>
      <th>Actions</th>
    </tr>
    <% @subjects.each do |subject| %>

    <tr class="<% cycle('odd', 'even')%>">
      <td><%= subject.position %></td>
      <td><%= subject.name %></td>
      <td class="center"><%= status_tag(subject.visible)%></td>
      <td class="center"><%= subject.pages.size %></td>
      <td class="actions">
        <%= link_to("View Pages", {:controller => 'pages', :subject_id => subject.id}, :class => 'action view page') %>
        <%= link_to("Show", {:action=>'show', :id => subject.id}, :class => 'action show') %>
        <%= link_to("Edit", {:action => 'edit', :id=>subject.id}, :class => 'action edit') %>
        <%= link_to("Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete') %>
      </td>
    </tr>
    <% end %>
  </table>
</div>

然后我在我的页面控制器中为:subject_id创建了一个before操作,以找到具有该id的主题,然后使用该id显示页面。我的页面控制器代码:

class PagesController < ApplicationController

  layout "admin"

  before_action :confirm_logged_in
  before_action :find_subject

  def index
    #@pages = Page.sorted
    @pages = Page.where(:subject_id => @subject.id)
    #@pages = @subject.pages
  end

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

  def new

    @page = Page.new(:subject_id => @subject.id)
    @subjects = Subject.order('position ASC')
    @page_count = Page.count + 1
  end

  def create
    @page = Page.new(page_params)
    if @page.save
      flash[:notice] = "Page created successfully."
      redirect_to(:action => 'index', :subject_id => @subject.id)
    else
      @subjects = Subject.order('position ASC')
      @page_count = Page.count + 1
      render('new')
    end
  end

  def edit
    @page = Page.find(params[:id])
    @subjects = Subject.order('position ASC')
    @page_count = Page.count
  end

  def update
    @page = Page.find(params[:id])
    if @page.update_attributes(page_params)
      flash[:notice] = "Page updated successfully."
      redirect_to(:action => 'show', :id => @page.id, :subject_id => @subject.id)
    else
      @subjects = Subject.order('position ASC')
      @page_count = Page.count
      render('edit')
    end
  end

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

  def destroy
    page = Page.find(params[:id]).destroy
    flash[:notice] = "Page destroyed successfully."
    redirect_to(:action => 'index', :subject_id => @subject.id)
  end


  private

    def page_params
      params.require(:page).permit(:subject_id, :name, :permalink, :position, :visible)
    end

    def find_subject
      if params[:subject_id]
      @subject = Subject.find(params[:subject_id])
      end
    end

end

我还定义了一个在我的页面模型中排序的范围,以根据页面的位置对页面进行排序。 型号代码:

class Page < ActiveRecord::Base

    belongs_to :subject
    has_many :sections
    has_and_belongs_to_many :editors, :class_name => "AdminUser"

  validates_presence_of :name
  validates_length_of :name, :maximum => 255
  validates_presence_of :permalink
  validates_length_of :permalink, :within => 3..255
  # use presence_of with length_of to disallow spaces
  validates_uniqueness_of :permalink
  # for unique values by subject use ":scope => :subject_id"

    scope :visible, lambda {where(:visible=>true)}
  scope :invisible, lambda {where(:visible=>false)}
  scope :sorted, lambda {order("pages.position ASC")}
  scope :newest_first, lambda {order("pages.created_at ASC")}
  scope :search, lambda {|query|
    where (["name LIKE ?", "%#{query}%"])
  }
end

但是当我使用@pages = Page.where(:subject_id =&gt; @ subject.id).sorter或@pages = @ subject.pages.sorted时,这个范围似乎不起作用。当我使用它时它什么都没显示,当我删除sort时,如上面的代码所示,我可以正确地看到页面但没有排序。我不知道为什么会这样。但是当我使用@pages = Page.sorted时它会起作用。从索引的第2和第3个命令中排除的删除也是无处不在的。我没有真正的解释为什么我删除它。

Anyhelp在这里解释这个问题将不胜感激。谢谢。

0 个答案:

没有答案