2012-07-22 36 views
0

我创建使用Rails 3.2.5一个博客,并利用途径产生的(即我code_entriescode_entries_pathnew_code_entry_path等)中的资源路径已经使用link_to的每一个动作整个网站。突然每当我试图访问一个页面我得到这个错误:路线生成的路径已经停止工作

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

此错误是从我index行动我code_entries_controller.rb,但无论哪个动作我尝试去到任何控制器我得到这个带有路由资源路径的每个link_to错误。它之前工作正常,我不确定会造成这种情况。

由于这个问题是我code_entries的情况下,这里是code_entries_controller.rb/code_entries/index.html.hamlroutes.rb代码,我已经得到了错误,但在行动code_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 

显示会话转储

显示ENV转储

响应

页眉:

None 

回答

0

我在application_controller.rb里面做了一个方法,并用它作为参数helper_method。由于它在application_controller.rb之内,而不是application_helper.rb我使用了标签include ActionView::Helpers::UrlHelper,所以我可以在帮助器方法中使用link_to。不知怎的,include声明使它炸毁,但一旦我删除include声明,并将帮助器方法移动到application_helper.rb事情变得很好!