NoMethodError在Rails中构建一个简单的CMS

时间:2014-05-22 20:34:46

标签: ruby-on-rails ruby nomethoderror

我一直关注this tutorial from lynda.com

我在第15章:在主题中嵌套页面

所以我已经跟进,甚至只是复制他们提供给你的运动文件。但是我一直收到这个错误:

PagesController#new中的NoMethodError 未定义的方法`id'为零:NilClass

它点亮了第二行:

def new
  @page = Page.new({:subject_id => @subject.id, :name => "Default"})
  @subjects = Subject.order('position ASC')
  @page_count = Page.count + 1
end

这是我的控制器

class PagesController < ApplicationController

  layout "admin"

  before_action :confirm_logged_in
  before_action :find_subject

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

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

  def new
    @page = Page.new({:subject_id => @subject.id, :name => "Default"})
    @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
    # Find an existing object using form parameters
    @page = Page.find(params[:id])
    # Update the object
    if @page.update_attributes(page_params)
      # If update succeeds, redirect to the index action
      flash[:notice] = "Page updated successfully"
      redirect_to(:action => 'show', :id => @page.id, :subject_id => @subject_id)
    else
      # If the update fails, redisplay the form so user can fix problems
      @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 '#{page.name}' destroyed successfully"
    redirect_to(:action => 'index', :subject_id => @subject_id)
  end

  private

    def page_params
      # Same as using "params[:subject]", except that it:
      # - raises as error if :subject is not present
      # - allows listed attributes to be mass-assigned
      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

然后是观点:

<% @page_title = "New Page" %>

<%= link_to("<< Back to list", {:action => 'index', :subject_id => @subject_id}, :class => 'back-link') %>

<div class="pages new">
    <h2>Create Page</h2>

    <%= form_for(:page, :url => {:action => 'create', :subject_id => @subject_id}) do |f| %>

      <%= render(:partial => "form", :locals => {:f => f}) %>

      <div class="form-buttons">
        <%= submit_tag("Create page") %>
      </div>

    <% end %>
</div>

类似于has been asked here的问题,但我在那里看不到任何解决方案。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

我认为问题在于

Page.new({:subject_id => @subject.id, :name => "Default"})

@subject未定义,您要求其ID

答案 1 :(得分:0)

你必须定义@subject。

将此作为新动作的第一行

@subject = Subject.find(params[:id])

现在@subject是未定义的因此它是nil,在nil上调用.id导致你看到的错误。