안녕하세요, 저는 루비를 처음 사용하면서 지금까지 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에 액세스 할 수는 있지만 괜찮 으면 시도 할 수 없습니다. 왜 그런가요? 어떻게 고쳐야합니까?
를 (X == true)가'거의 항상 무의미한 운동의 경우 메모'으로. 그것은 사실이거나 그렇지 않습니다. 'if (x)'충분하다. – tadman
나는 그저 시험으로 사용하고 있다는 것을 알고있다. – Noob
진단을하고 있다면,'comment.attempted.inspect'가 정확히 무슨 일이 벌어지는 지 확인하는 가장 좋은 방법입니다. 그것은 그렇지 않으면 보이지 않을 빈 문자열이라 할지라도 당신이 다루는 것을 정확히 보여 줄 것입니다. 'to_str'을 호출하는 것은 드문 경우지만, 'to_s'는 거의 항상 선호됩니다. – tadman