0

我的观点:

<%= form_tag(rate_url) do %>
  <%= hidden_field_tag :prod_id, params[:product_id] %>
  <%= hidden_field_tag :rating_set_id, params[:rating_set_id] %>
  <span class = "heading"> Recommendations </span>
  <div><%= submit_tag 'Submit Ratings', :class => " btn right" %></div>
  <span id = "rate_more_link">
    <%= link_to "Rate More Products", products_path(:rating_set_id => params[:rating_set_id])%>
  </span>
  <br/>
  <div id ="prods_container">
  <% @recommendations.each do |rec| %>
    <% url_raw = URI.parse("url_to_parse") %>
    <% url = Net::HTTP.get_response(url_raw).body %>
    <% if url.empty? %>
      <% @title          = "Product Unavailable via API" %>
      <% @url            = "#{rec.wmt_id}" %>
      <% @cover_img      = "_180X180.jpg" %>
      <% @price          = "Product Unavailable via API" %>
    <% else %>
      <% begin %>
        <% @response1  = JSON.parse(url) %>
        <% @title          = @response1["ProductName"]%>
        <% @url            = "{@response1["ProductUrl"]}"%>
        <% @cover_img      = @response1["ImagePath"]%>
        <% @price          = @response1["currentItemPrice"]%>
      <% rescue %>
      <% end %>
    <% end %>
    <div id ="prod">
      <span class = "radio_button">
        <%= hidden_field_tag "recommendation_ratings[#{rec.id}][recommendation_id]", rec.id %>
        <%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '3'%> Good
        <%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '2'%> Fair
        <%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '1'%> Bad
        <%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '0'%> N/A
      </span>
    <div>
      <a href='<%= @url %>' target="_blank">
        <img src='<%= @cover_img  %>' class='product_image_rec_table'></img>
      </a>
    </div>
    <div class = "rec-desc">
      <div class = "small"><b>Wmt ID: </b><%= rec.wmt_id  %></div>  
      <div class = "small"><b>Title: </b><%= @title  %></div>
      <div class = "small"><b>Price: </b>$<%= @price  %></div>
      <div class = "em">
        <b>Current Rating: </b>
        <% rec.ratings.each do |rating_phin| %>
          <%= rating_phin.label %>
        <% end %></div>
        <br/>
      </div>
      <div id="rec_note_text">
        <%= text_field_tag "recommendation_ratings[#{rec.id}][notes]", "#{rec.notes}" ,:id => "rec_note_text", :placeholder => 'Enter Notes..'%>
      </div>
    </div>
    <% end %>
  <% end %>
</div>

我试图将块<% end %>Current Rating块移动到末尾,以便我可以rating_phin在我的变量中调用变量text_field_tag,但是当我将块移动<% end %>到视图的末尾时,Current Rating块之后的所有内容都不会被渲染。我究竟做错了什么?如何在视图结束时更新rating_phin.notes而不是rec.notes在我的视图中?text_field_tag

4

1 回答 1

0

rating_phin变量仅存在于该each循环的范围内。

如果您稍后需要它,您将不得不再次循环:

<div id="rec_note_text">
  <% rec.ratings.each do |rating_phin| %>
    <%= text_field_tag "recommendation_ratings[#{rec.id}][notes]", "#{rec.notes}" ,:id => "rec_note_text", :placeholder => 'Enter Notes..'%>
  <% end %>
</div>

我不确定为什么您会期望移动<% end %>标签是正确的做法。

如果您迷失在 ERB 中,考虑到 HTML 会变得多么繁琐和复杂,这并不是特别困难,尤其是当其中包含实时 Ruby 时,您可能想尝试HAML。它将减少到大约一半的行数,并删除许多<% %>往往使 ERB 难以阅读的结束标签碎片。

于 2013-02-20T04:43:14.713 回答