2012-02-16 60 views
2

我有以下articles_controller:期望的迴應是成功的,但302

def myarticles 
    @myarticles = current_student.articles.all 
    respond_to do |format| 
     format.html 
     format.xml { render :xml => @myarticles } 
    end 
end 

def create 
    @article = current_student.articles.new(params[:article]) 
    respond_to do |format| 
    if @article.save 
     format.html { redirect_to(@article, :notice => 'επιτυχώς.') } 
     format.xml{render:xml => @article, :status => :created, :location => @article} 
    else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @article.errors, :status => :unprocessable_entity} 
    end 
    end 
end 

我的終端rake routes上給了我:

myarticles_articles GET /articles/myarticles(.:format) {:action=>"myarticles", :controller=>"articles"} 

我的config/routes.rb中是

Sample::Application.routes.draw do 
    devise_for :students 

    resources :articles do 
    collection do 
     get 'about' 
     get 'all' 
     get 'myarticles' 
    end 
end 

root :to => 'articles#index' 
end 

和我的觀點是在/app/views/articles/myarticles.html.erb

當我的瀏覽器我瀏覽到

http://127.0.0.1:3000/articles/myarticles 

我有錯誤:

Template is missing 

Missing template articles/myarticles with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/panagiotis/projects/sample/app/views", "/home/panagiotis/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views" 

,當我運行耙從終端具有以下articles_test_controller內容:

test "should get myarticles signed in" do 
    get :myarticles 
    assert_response :success 
end 

我得到失敗

Expected response to be a <:success>, but was <302> 

我讀了重定向到,但我似乎無法解決這個問題。

+0

在文件myarticles.html.erb屬性,權限:我是所有者,我有讀寫權限。 – 2012-02-16 22:47:36

+0

我很蠢。這裏wtite myarticles和目錄寫myaricles。簡直難以置信! – 2012-02-16 23:41:59

回答

1

要調試失蹤模板的問題,你可以嘗試這樣的:

format.html { render 'myarticles' } 

關於您的規格失敗,您可能需要在測試中擁有當前用戶會話,否則會認爲您是註銷用戶並將您重定向到登錄頁面。我沒有使用設計,但它看起來像自述文件中的「測試助手」下的一些建議:https://github.com/plataformatec/devise

+0

我做了這個改變:在articles_controller.rb放在respond_to之後| format | - > format.html {render'myarticles'}和articles_controller_test.rb在測試後放置「應該得到myarticles簽名」do - > sign_in students(:student2)(我有test/fixtures/students.yml student2) 。將我的模板從路徑/app/views/articles/myarticles.html.erb複製粘貼到/app/views/myarticles.html.erb,但現在我的錯誤(來自失敗)與描述ActionView :: MissingTemplate:缺少模板文章/ myarticles with {:handlers => [:erb,:rjs ... – 2012-02-16 22:43:00

相關問題