2014-04-10 3 views
0

브라우저 콘솔 또는 페이지에 메시지를 인쇄하여 각 메서드가 호출 될 때 정확히 볼 수 있습니다. 아무것도 인쇄되고 있지 않으며 작동하는 해결책을 찾지 못했습니다. 플래시 메시지를 사용해 보았지만 제대로 작동하지 않았습니다. 나는 레일즈를 배우려고하고 있는데, 정확히 무엇이 호출되는지를보고 싶습니다. 예를 들어 create가 호출되면 "Create was called!"라는 메시지를보고 싶습니다. 나는 이것을 시도했다 :메서드가 호출 될 때 콘솔에 출력 하시겠습니까?

class PostsController < ApplicationController 

    # only index, show, new, and edit pages will have views 
    # create, update, and destroy will do some kind of work and then redirect 
    # instance variables are visible in our view files 
    def index 
    @posts = Post.all 
    end 

    def show 
    # find the post and find the id that was passed into the url 
    @post = Post.find(params[:id]) 
    flash[:success] = "SHOW WAS CALLED IN FLASH" 
    puts "SHOW METHOD WAS CALLED!" 

    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(params[:post]) 
    if @post.save 
     redirect_to(posts_path, :notice => "Your post was saved!") 

    else 
     render "new" 
    end 
    end 

    def edit 
    puts "<p>EDIT METHOD WAS CALLED!</p>" 

    @post = Post.find(params[:id]) 
    end 

    def update 
     puts "UPDATE METHOD WAS CALLED!" 

    @post = Post.find(params[:id]) 

    if @post.update_attributes(params[:post]) 
     redirect_to posts_path, :notice => "Your post has been saved." 
    else 
     render "edit" 
    end 
    end 

    def destroy 
    puts "DESTROY METHOD WAS CALLED!" 

    @post = Post.find(params[:id]) 
    @post.destroy 

    redirect_to(posts_path, :notice => "Post deleted") 
    end 


end 

답변

0

컨트롤러에 puts를 사용하면 브라우저의 웹 페이지에 인쇄되지 않는다. Webrick을 사용하는 경우 서버가 실행중인 터미널에 인쇄됩니다. 당신이 웹 페이지를 인쇄 할 경우,이 같은 조치에 해당보기에 그렇게해야합니다

쓰기 show.html.erb 에서 :

Show 메서드가 호출!

액션의 Rails.logger를 사용하여 로그 파일에 문을 기록 할 수 있습니다.

Rails.logger.info "SHOW METHOD WAS CALLED!" 

응용 프로그램이 실행되는 환경에 따라 development.log/production.log 파일에 기록됩니다.

2

사용이에 대한 로거와 꼬리 log/development.log

logger.debug "Method was called" 

debug 수준 로깅은 개발 환경에서 기록됩니다. 생산에 로그인하려면 info을 사용하십시오.

관련 문제