Expected response to be a success, but was 302

若如初见. 提交于 2019-12-10 18:36:44

问题


I have the following articles_controller:

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

on my terminal rake routes gives me:

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

my config/routes.rb is

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

and my view is in /app/views/articles/myarticles.html.erb

When on my browser I navigate to

http://127.0.0.1:3000/articles/myarticles

I have the error:

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"

and when I run rake from the terminal with the following articles_test_controller content:

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

I get the failure

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

I read about redirect to but I can't seem to fix that issue.


回答1:


To debug your missing template issue, could you try something like:

format.html { render 'myarticles' }

?

Regarding your specs failure, you likely need to have a current user session in your tests, otherwise it thinks you're a logged out user and redirects you to a login page. I haven't used Devise but it looks like the README has some suggestions under "Test Helpers" : https://github.com/plataformatec/devise



来源:https://stackoverflow.com/questions/9315992/expected-response-to-be-a-success-but-was-302

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!