-1

@original_id等しいと比較するためにandを文字列に変換する必要があるようcomment.idですが、それらを文字列に変換しないと、elseブランチ内の文字列が返されます。どうしてこれなの?そして、それを回避する方法はありますか?

- if @original_id.to_s == comment.id.to_s
    = "Matched"
- else
    = "hi"

環境:

.comment{:class => "c" + nesting.to_s}
    .profile
        %img{:src => "/assets/profile_image_sample.jpg"}
    .message
        .username
            - if comment.user.username.blank?
                = comment.user.first_name
            - else
                = comment.user.username 
        = comment.content
        .reply-link
            = link_to "Reply to comment...", post_path(:original_id => comment.id)
            = @original_id.to_s + "and" + comment.id.to_s
- if @original_id.to_s == comment.id.to_s
    = "Matched"
- else
    = "hi"
- if comment.replies.count > 0
    - nesting = nesting + 1
    - comment.replies.each do |comment|
        = render "comment", :comment => comment, :nesting => nesting
4

1 に答える 1

2

これら 2 つの変数の 1 つは文字列で、もう 1 つは数値です。

考えられる原因は、コントローラで受信 (ポスト) されたすべてのパラメータが文字列であることです。明示的な変換を行わないと、この値を文字列としてビューに渡します。

2番目の質問に答えるには:

@original_id をコントローラーで設定するときに整数に変換します。

@original_id = params[:original_id].to_i
于 2013-09-24T10:04:37.160 に答える