2016-04-03 19 views
-1

kontrolörü test ederken hata kodunu tamsayı ActionController :: Parametreler döküm olamaz. Ne zaman test/denetleyicileri/articles_controller_test.rb dosyamı test etmeye çalıştığımda bu hata iletisini alıyorum.TypeError: Şu anda Raylar denetleyicileri test edip takıldım öğrenmek çalışıyorum Raylar 4.1.10

ArticlesControllerTest#test_should_update_article: 
    TypeError: can't cast ActionController::Parameters to integer 
    app/controllers/articles_controller.rb:67:in `update' 
    test/controllers/articles_controller_test.rb:50:in `block in <class:ArticlesControllerTest>' 

articles_controller_test.rb dosyası şudur:

require 'test_helper' 

class ArticlesControllerTest < ActionController::TestCase 
    setup do 
    @article = articles(:welcome_to_rails) 
    end 

    test "should get index" do 
    get :index 
    assert_response :success 
    assert_template 'index' 
    assert_not_nil assigns(:articles) 
    end 

    test "should get new" do 
    login_as(:eugene) 
    get :new 
    assert_response :success 
    end 

    test "should create article" do 
    login_as(:eugene) 
    assert_difference('Article.count') do 
     post :create, :article => { :title => 'Post title', 
            :body => 'Lorem ipsum..' } 
    end 

    assert_response :redirect 
    assert_redirected_to article_path(assigns(:article)) 
    end 

    test "should show article" do 
    get :show, :id => @article.to_param 

    assert_response :success 
    assert_template 'show' 

    assert_not_nil assigns(:article) 
    assert assigns(:article).valid? 
    end 

    test "should get edit" do 
    login_as(:eugene) 
    get :edit, :id => @article.to_param 
    assert_response :success 
    end 

    test "should update article" do 
    login_as(:eugene) 
    **put :update, :id => @article.to_param, :article => { :title => 'New Title' }** 
    assert_redirected_to article_path(assigns(:article)) 
end 

    test "should destroy article" do 
    login_as(:eugene) 
    assert_nothing_raised { Article.find(@article.to_param)} 

    assert_difference('Article.count', -1) do 
     delete :destroy, :id => @article.to_param 
    end 
    assert_response :redirect 
    assert_redirected_to articles_path 

    assert_raise(ActiveRecord::RecordNotFound) { Article.find(@article.to_param) } 
    end 
end 

koşuyorum Raylar 4.1.10 ve benim hata Güçlü Parametreler gem ile bir ilgisi vardır eminim o Raylar şimdi kullanıyor. Birisi bana problemimi kodumla gösterebilseydi ve bunun neden yanlış olduğunu açıklayabiliyor olsaydı büyük beğeni topladı.

Bu, hat 50 ve hat 67 sırasıyla yıldızlarda gerçek article_controller dosya özetlenmiştir edilir: Eğer sınıf dışında article_params yöntemini belirleyen gibi

class ArticlesController < ApplicationController 
    before_filter :authenticate, :except => [:index, :show] 

    def index 
    @articles = Article.all 

    respond_to do |format| 
     format.html 
     format.xml { render :xml => @articles } 
    end end 
    def show 
    @article = Article.find(params[:id]) 

    respond_to do |format| 
     format.html 
     format.xml { render :xml => @article } 
    end 
    end 

    def new 
    @article = Article.new 

    respond_to do |format| 
     format.html 
     format.xml { render :xml => @article } 
    end 
    end 

    def edit 
    @article = current_user.articles.find(params[:id]) 
    end 

    def create 
    @article = current_user.articles.new(article_params) 

    respond_to do |format| 
     if @article.save 
     format.html { redirect_to(@article, :notice => 'Article was successfully created.') } 
     **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 

    def notify_friend 
    @article = Article.find(params[:id]) 
    Notifier.email_friend(@article, params[:name], params[:email]).deliver 
    redirect_to @article, :notice => "The message has been sent to your friend" 
    end 

    def update 
    **@article = current_user.articles.find(article_params)** 

    respond_to do |format| 
     if @article.update_attributes(params[:article]) 
     format.html { redirect_to(@article, :notice => 'Article was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @article.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @article = current_user.articles.find(params[:id]) 
    @article.destroy 

    respond_to do |format| 
     format.html { redirect_to(articles_url) } 
     format.xml { head :ok } 
    end 
    end 

end 

def article_params 
    params.require(:article).permit(:title, :location, :categories, :excerpt, :body, :published_at) 
end 
+0

nasıl kontrolör (- soran benim nedenini anlamak için hata iletisi görebilirsiniz 'update' özellikle hat' 67 ') benziyor? Ve testinizde '50' satırı nedir (ben sadece saymak istemiyorum ...), lütfen bu çizgiyi işaretleyebilir misiniz? – spickermann

+0

Şunları denediniz mi: update,: id => @ article.id? – margo

+0

Evet ben sadece denedim @margo ve yıldızlarla hattını 50 özetlenen ve hat 67 ayrıca gönderin Can bana – AdamG

cevap

0

görünüyor. Sadece sana

def create 
    @article = current_user.articles.new(article_params) 

def create 
    @article = current_user.articles.build(article_params) 
için denetleyici içinde bu satırı değiştirmek gerektiğini düşünüyorum

end 

def article_params 
    params.require(:article).permit(:title, :location, :categories, :excerpt, :body, :published_at) 
end 

Dahası

def article_params 
    params.require(:article).permit(:title, :location, :categories, :excerpt, :body, :published_at) 
    end 
end 

ila Dosyanda son beş satırları değiştirmek

Hangi yöntemlere bakın has_many yöntemleri, bir sınıfa ekler: build, ...

+0

(emin kesim aşağı örneği hala başarısız olun) bu yüzden sayesinde bu çalıştı ve ben cevabımı güncellenen aynı mesajı – AdamG

+0

@AdamG var ... – spickermann

+0

ben yaptım ve sonra kodu ile biraz etrafına oynanan ve nihayet çalıştı çok! – AdamG

0

Sınıf kaydı, yeni bir kayıt döndüren yönteme yanlış bir yerde. Article_params yöntem tanımından önce taşıyın.

+0

Denedim ve ben bunu denedim @spickermann hala aynı hata mesajını – AdamG

+0

gösterdi ve aynı mesajı döndürdü – AdamG