私はいくつかのコードを必要とし、DRYメソッドに従ってください(繰り返しません)。このピースを使用してエスケープし、5回またはもっと。ですから、私はrubyとhtmlの両方を含むメソッドを定義し、htmlとjsのrubyにはコードを定義します。しかし、私は実現に問題があります。html to railsメソッドをインポートする(helper.rb内)
マイhtml.erbは次のようになります。
<% div_count = 1 %>
<div>
<% for post in @posts %>
<div class="post-on-main-page-<%= div_count %>"
id="js-count-<%= div_count_for_js %>">
<script>
$("#js-count-<%= div_count_for_js %>").click(function(){
window.location.href = "<%= post_url(post) %>";
});
</script>
<%= image_tag(post.picture.url) %>
<span><h5><%= post.theme %></h5></span>
</div>
<% end %>
<% div_count += 1 %>
<% if div_count == 4 %>
<% div_count = 1 %>
<% end %>
</div>
そして、私はメソッドを作成したい、私はapplication_helper.rbのコードのこの部分をどうすると思います。私はこれが動作することができますどのよう
<%= method_name(@posts)%>
html.erb
のために何をしたいdef method_name(parameter)
<% div_count = 1 %>
<div>
<% for post in parameter %>
<div class="post-on-main-page-<%= div_count %>"
id="js-count-<%= div_count_for_js %>">
<script>
$("#js-count-<%= div_count_for_js %>").click(function(){
window.location.href = "<%= post_url(post) %>";
});
</script>
<%= image_tag(post.picture.url) %>
<span><h5><%= post.theme %></h5></span>
</div>
<% end %>
<% div_count += 1 %>
<% if div_count == 4 %>
<% div_count = 1 %>
<% end %>
</div>
end
結果:どのようなことのようになりますか?
利用パーシャルhttp://guides.rubyonrails.org/layouts_and_rendering.html#using-partials – abhi110892