2015-01-21 1 views
0

안녕하세요, 저는 루비를 처음 사용하면서 지금까지 this tutorial을 완성했습니다. 나는 현재 내가 게시 한 것처럼 누군가에게 의견을 이메일로 보낼 수 있도록 기능을 추가하려고합니다.뷰에서 모델 클래스의 자체 제작 변수 사용?

필자가 볼 수없는 부분은 필자가 내 의견 클래스 (bd에서)에 설정 한 변수를 인식 할 수있게하는 것입니다.

아래 코드에 이메일 주소가 입력되었는지 확인하려고합니다. 그럴 경우 전송하려고 시도합니다. 그렇지 않으면 평소와 같이 주석을 게시합니다. 이메일이 입력되었는지 확인하기 위해 부울을 설정했습니다. 그런 다음 _comment.html.erb에서 액세스 할 수 없습니다. 왜 이런거야?

comment.html.erb

<p> 
    <strong>Commenter:</strong> 
    <%= comment.commenter %> 
</p> 

<p> 
    <strong>Comment:</strong> 
    <%= comment.body %> 
</p> 
<p> 
<%= comment.attempted.to_str %> <==== this line crashes as attempted is a nil class 
</p> 

<% if(comment.attempted == true) %> 
    <p> 
    your email was sent 
    </p> 
    <% end %> 



<p> 
    <%= link_to 'Destroy Comment', [comment.article, comment], 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %> 
</p> 

_form.html.erb는

<p> 
    <strong>Commenter:</strong> 
    <%= comment.commenter %> 
</p> 

<p> 
    <strong>Comment:</strong> 
    <%= comment.body %> 
</p> 
<p> 
<%= comment.body.to_str %> 
</p> 

<% if(comment.attempted == true) %> 
    <p> 
    your email was sent 
    </p> 
    <% end %> 



<p> 
    <%= link_to 'Destroy Comment', [comment.article, comment], 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %> 
</p> 

comments_controller.rb (이것은 초기 코멘트를 구축 것입니다)

class CommentsController < ApplicationController 
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy 

    def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.create(comment_params) 
    @comment.attempted = false 


    if([email protected]?) 
    @comment.attempted = true 
    if(@comment.email.include? '@') 
    UserMailer.comment_email(@comment.email,@comment.body).deliver_now 
    end 
    end 
    redirect_to article_path(@article) 
    end 

_comment.html.erb에서주의하십시오. comment.boby 및 comment.commenter에 액세스 할 수는 있지만 괜찮 으면 시도 할 수 없습니다. 왜 그런가요? 어떻게 고쳐야합니까?

+0

를 (X == true)가'거의 항상 무의미한 운동의 경우 메모'으로. 그것은 사실이거나 그렇지 않습니다. 'if (x)'충분하다. – tadman

+0

나는 그저 시험으로 사용하고 있다는 것을 알고있다. – Noob

+0

진단을하고 있다면,'comment.attempted.inspect'가 정확히 무슨 일이 벌어지는 지 확인하는 가장 좋은 방법입니다. 그것은 그렇지 않으면 보이지 않을 빈 문자열이라 할지라도 당신이 다루는 것을 정확히 보여 줄 것입니다. 'to_str'을 호출하는 것은 드문 경우지만, 'to_s'는 거의 항상 선호됩니다. – tadman

답변

0

여기서 가장 명백한 문제는 댓글에 대한 변경 사항을 저장하지 않는다는 것입니다.

레코드의 주석을 반복 할 때 데이터베이스에서 모델을 다시 생성하면 별도의 인스턴스입니다.

데프 @article = Article.find ([: article_id를] PARAMS) 작성 : 당신은 당신의 방법에 루프가 같은

# Prepare but do not save a new model 
@comment = @article.comments.build(comment_params) 
@comment.attempted = false 

# ... Stuff that may alter @comment.attempted 

# Save the new instance. 
@comment.save! 
+0

그래, 그게 다야. 고맙습니다. 그런 기본적인 실수는 있었지만 아마 그것을 잡지 못했을 것입니다. 어쨌든 해결책과 멋진 설명에 감사드립니다. – Noob

0

보이는 :

가능성이 가장 높은 수정이가 를 @ comment = @ article.comments.create (comment_params)

이것은 create 함수를 다시 호출합니다. @

= comment.attempted 거짓

가 컨트롤러에 설정하지 않기 때문에보기에 '전무'을 해결하는 이유 comment.attempted의

: 즉 코드는 다음 줄에 할당 도달하지 않았다.

그래서처럼 다시 것 :

def create 
    @article = Article.find(params[:article_id]) 
    @comment.attempted = false 

    if([email protected]?) 
    @comment.attempted = true 
    if(@comment.email.include? '@') 
     UserMailer.comment_email(@comment.email,@comment.body).deliver_now 
    end 
    end 

    # This line adds the comment instance to the article's comments collection. 
    @article.comments << @comment 

    redirect_to article_path(@article) 
end 
관련 문제