2016-03-20 23 views
-2

私はレールが初めてで、フレームワークに慣れるための小さなプロジェクトを作成しようとしています。私は現在、平均評価が与えた星の数(Amazonの星評価システムのようなもの)を示す非常に単純なビューを作成しようとしています。この1つの問題は私が自分自身を理解することはできません。私は "stars_view"を構築しましたが、レールコードはHTMLをレンダリングしません。それはおそらく初心者の間違いですが、私はそれを数日間見つけられませんでした。レンダリングでHTMLコードがレンダリングされない

これは私が部分的ビューをレンダリングする方法である:

<div class="col-sm-8"> 
    <h2><%= @document.title %></h2> 
    <%= render :partial=>'application/stars_view', :locals => {:number_of_stars => @document.average_rating_number_of_stars} %> 
    <br/>von 
    <%= @document.user.email %> 
     <br/> 
     <p> 
     <%= @document.description %> 
     </p> 
</div> 

これは私の部分のコードです:

<div id="stars_view"> 
    Stars 
    <%= 
    #this link is for test purposes but even this does not show. 
    link_to 'Back', documents_path 
    rounded_number_of_stars = (number_of_stars.to_int*2.0)/2.0 
    max_number_of_stars = 5 
    drawn_number_of_stars = 0 
    while rounded_number_of_stars >= 1 do 
     image_tag("stars/star_full.png", :class => "img-responsive") 
     rounded_number_of_stars -= 1 
     drawn_number_of_stars += 1 
     #byebug stops here 
     #byebug 
    end 
    if rounded_number_of_stars == 0.5 
     image_tag("stars/star_half.png") 
     drawn_number_of_stars += 1 
     #and here 
     #byebug 
    end 
    while drawn_number_of_stars < max_number_of_stars do 
     image_tag("stars/star_empty.png") 
     drawn_number_of_stars += 1 
     #and here 
     #byebug 
    end 
    %> 
</div> 

これは私がSafariで取得するHTMLコードです:

<div id="stars_view"> 
    Stars 

</div> 

私はおそらくビュー自体にいくつかのバグがあることを知っています。私は後でそれらを解決します。今のところ、あらゆるHTMLを生成するためのヘルプを作成すると役立ちます。部分はapplication/_stars_view.html.erbとして保存され、すべての画像も適切な場所に置かれます。

答えて

2

<%=構文は、コードが返す最後のものだけを出力します。その中のすべてではありません。そのため、出力がないのがなぜですか。

画面に表示するすべてのものは、自分の<%=タグである必要があります。 任意のコードを実行するには<%を使用してください。例えば

<%- while rounded_number_of_stars >= 1 do %> 
    <%= image_tag("stars/star_full.png", :class => "img-responsive") %> 
    <% rounded_number_of_stars -= 1 
     drawn_number_of_stars += 1 
    %> 
<% end %> 

エトセトラ。

関連する問題