0

我正在创建一个博客,Rails 3.2.5并且一直在使用's 来使用整个站点中link_to由 Routes 生成的资源路径(即我的code_entries: code_entries_path、等)的每个操作。new_code_entry_path突然,每当我尝试访问一个页面时,我都会收到此错误:

undefined local variable or method `controller' for #<CodeEntriesController:0x007f887f8b3300>

此错误来自我index对 my 的操作code_entries_controller.rb,但无论我尝试在任何控制器中执行哪个操作,我都会在每个link_to带有 Routes 资源路径的操作上收到此错误。它以前工作正常,我不确定是什么原因造成的。

由于这个问题是在我的上下文中code_entries,所以这里是代码code_entries_controller.rb,以及我得到的错误,但在操作的上下文/code_entries/index.html.haml中:routes.rbcode_entries#index


code_entries_controller.rb


class CodeEntriesController < ApplicationController

  before_filter :authenticate_action

  def index
    @entries = CodeEntry.order("created_at desc")
  end

  def new
    @entry = CodeEntry.new
  end

  def show
    @entry = CodeEntry.find_by_id(params[:id])
  end

  def create
    @entry = CodeEntry.new(params[:code_entry])

    if @entry.save
      redirect_to code_entry_path(@entry), :notice => "#{@entry.title} has been created"
    else
      puts @entry.errors.messages.inspect
      render "new"
    end
  end

  def edit
    @entry = CodeEntry.find_by_id(params[:id])
  end

  def update
    @entry = CodeEntry.find_by_id(params[:id])
    if @entry.update_attributes(params[:code_entry])
      redirect_to code_entry_path(@entry), :notice => "#{@entry.title} has been updated"
    else
      puts @entry.errors.messages.inspect
      render "edit"
    end
  end

  def destroy
    @entry = CodeEntry.find_by_id(params[:id])
    @entry.destroy
    redirect_to code_entries_path, :notice => "#{@entry.title} has been deleted"
  end
end


/code_entries/index.html.haml


%h1 Hello!
%br
- @entries.each do |e|
  %h2= link_to e.title, code_entry_path(e)
  %h3= e.updated_at
  = raw excerpt(e, 200)
  %br
  - if current_user
    = link_to "Edit", edit_code_entry_path(e)
    = button_to "Delete", code_entry_path(e), :confirm => "Really really?", :method => :delete
  %hr


routes.rb


Blog::Application.routes.draw do
  resources :users
  resources :code_entries
  resources :food_entries
  resources :inclusive_entries
  resources :sessions

  root :to => "home#index"

  get "login" => "sessions#new", :as => "login"
  get "logout" => "sessions#destroy", :as => "logout"

  get "users/new"
end


NameError in Code_entries#index


显示 /Users/kyle/Projects/blog/app/views/code_entries/index.html.haml 其中第 4 行提出:

undefined local variable or method `controller' for #<CodeEntriesController:0x007f887f813418>

提取的源代码(第 4 行附近):

1: %h1 Hello!
2: %br
3: - @entries.each do |e|
4:   %h2= link_to e.title, code_entry_path(e)
5:   %h3= e.updated_at
6:   = raw excerpt(e, 200)
7:   %br
Rails.root: /Users/kyle/Projects/blog

应用程序跟踪 | 框架跟踪 | 全跟踪

app/views/code_entries/index.html.haml:4:in `block in _app_views_code_entries_index_html_haml___3334012984247114074_70112115764360'
app/views/code_entries/index.html.haml:3:in `_app_views_code_entries_index_html_haml___3334012984247114074_70112115764360'
Request

参数:

None

显示会话转储

显示环境转储

回复

标题:

None
4

1 回答 1

0

我在内部创建了一个方法,application_controller.rb并将其用作helper_method. 因为它在里面application_controller.rb而不是application_helper.rb我已经使用了标签include ActionView::Helpers::UrlHelper所以我可以使用一个link_toinside 辅助方法。不知何故,那个include声明让它爆炸了,但是一旦我删除了这个include声明并将辅助方法移动到application_helper.rb一切都很好!

于 2012-07-23T02:33:19.337 回答