I have a rails application which has some following code
<ul class="sortable grid row">
<% #videouploads.each do |video_upload| %>
<% if video_upload.category == 'モノナビ' && video_upload.priority == 1 %>
<%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %>
<% end %>
<% end %>
</ul>
I have to write this code 10 times for video_upload.priority == 1 where priority changes from 1 to 10. How do I avoid code duplication or do I have what is the best solution ?
loop over the priorities
<% priorities = (1..10).to_a %>
<% priorities.each do |priority| %>
<ul class="sortable grid row">
<% #videouploads.each do |video_upload| %>
<% if video_upload.category == 'モノナビ' && video_upload.priority == priority %>
<%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %>
<% end %>
<% end %>
</ul>
<% end %>
<% (1..10).each do |inc| %>
<ul class="sortable grid row">
<% #videouploads.each do |video_upload| %>
<% if video_upload.category == 'モノナビ' && video_upload.priority == inc %>
<%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %>
<% end %>
<% end %>
</ul>
<% end %>
How about this ?
Related
Unexpected end when end exists, But I don't see where the issue is.
My terminal keeps giving me the error: ActionView::SyntaxErrorInTemplate (Encountered a syntax error while rendering template:
my profile.html.erb
<% if #users.image.present? %>
<%= image_tag #users.image %>
<% end %>
<strong><h1><%= #users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if #user == current_user %>
<%= link_to"Edit Profile", edit_user_registration_path(#user) %>
<% if user_signed_in? && !#user? %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= #users.posts.count %> Posts </div>
<p><%= #users.full_name %></p>
<p><%= #users.description %></p>
<p><%= link_to #users.website if #users.website.present? %></p>
<%= #posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
<% if user_signed_in? && #user == current_user %>
<%= link_to"Edit Profile", edit_user_registration_path(#user) %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
now it's ok :D
Seems like issue is here
<p><%= link_to #users.website if #users.website.present? %></p>
Change it to
<p><%= link_to 'User Website', #users.website if #users.website.present? %></p>
I have seen two mistakes, please correct them. first there is nothing #user? that is biggest error, another might not be problem but there is no space <%= link_to"Edit Profile" Please try following code I have changed things and reformat it properly.
<% if #users.image.present? %>
<%= image_tag #users.image %>
<% end %>
<strong><h1><%= #users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if #user == current_user %>
<%= link_to "Edit Profile", edit_user_registration_path(#user) %>
<% if user_signed_in? && !#user %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= #users.posts.count %> Posts </div>
<p><%= #users.full_name %></p>
<p><%= #users.description %></p>
<p><%= link_to #users.website if #users.website.present? %></p>
<%= #posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
I have the following loop to create a select dropdown using ERB. It is working correctly.
<%= f.select(:player_id) do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
My question is how do I add a class to the select element?
I have tried the following:
<%= f.select(:player_id), class: "form-control" do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
and
<%= f.select(:player_id), { class: "form-control" } do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
I have seen questions similar to this, but none that use a loop like the example above.
This should work for you.
<%= f.select :player_id, options_for_select( #players.collect { |player| ["#{player.first_name} #{player.last_name}", player.id] } ), class: 'form-control' %>
This is what I ended up doing:
<%= f.select :player_id, options_for_select( #players.collect { |player| ["#{player.first_name} #{player.last_name}", player.id] } ), {}, { class: 'form-control' } %>
I think problem with your code is f.select(:player_id).
You are calling the select method with only one parameter :player_id. So you can pass other options like this.
<%= f.select :player_id, class: "form-control" do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
I want to set up a Ruby partial that takes a 'type' then, depending on that type, spits out a <li> with defined elements (such as an icon, a particular label, etc)
Here's my working .erb chunk
<ul>
<% if locals.has_key? :sermon_links %>
<% sermon_links.each do |link| %>
<% if locals.has_key? :type == "download" %>
<li>
<i class="material-icons">music_note</i>
Download this sermon (~5mb)
</li>
<% else %>
<% if locals.has_key? :type == "passage" %>
<li>
<i class="material-icons">link</i>
<%= sermon_passage %> on Bible Gateway
</li>
<% end %>
<% end %>
<% end %>
</ul>
And then I'd call it in the HTML file like this:
:sermon_links => [
{ :type => "download", :hyperlink => "https://www.biblegateway.com/resources/matthew-henry/John" }
]
I'm 99% sure the problem is how I'm setting the 'IF type equals THIS' (<% if locals.has_key? :type == "download" %>) I'm a newbie to Ruby on Rails so any help in this area would be really appreciated :) thanks!
I think you can rewrite as follows:
<ul>
<% if locals.has_key? :sermon_links %>
<% sermon_links.each do |link| %>
<% if link[:type].present? && link[:type] == "download" %>
<li>
<i class="material-icons">music_note</i>
Download this sermon (~5mb)
</li>
<% else %>
<% if link[:type].present? && link[:type] == "passage" %>
<li>
<i class="material-icons">link</i>
<%= sermon_passage %> on Bible Gateway
</li>
<% end %>
<% end %>
<% end %>
<% end %>
</ul>
I've got a form to add SaleQualifiers to my app - which works fine when I'm using:
<%= form_for(#sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
The problem with form_for is that I want to put the form inline within a table row in my view - so as a method to get around that I'm now using:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
This is working fine for most of the fields I need to generate, but I've got a nested attribute field for Answer (Answer belongs_to SaleQualifier). This is not generating the right field names in the view, and as a result when I go to save the object this way I don't capture the answer_attributes.
Here's the full working form using form_for:
<div class="panel panel-default">
<%= form_for(#sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
<% if #sale_qualifier.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sale_qualifier.errors.count, "error") %> prohibited this answer from being saved:</h2>
<ul>
<% #sale_qualifier.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="panel-body">
<div class="col-sm-6">
<h2><%= #question.question_text %></h2>
<% unless #question.id == 1 %>
<p><%= link_to('Back', edit_sale_qualifier_path(id: #prior_sale_qualifier), data: { disable_with: "Loading..." }, :remote => true) %></p>
<% end %>
</div>
<div class="col-sm-6">
<div class="form-group">
<%= f.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
</div>
<div class="form-group">
<%= f.hidden_field :question_id, :value => #question.id %>
</div>
<% unless #question.id == 1 %>
<div class="form-group">
<%= f.hidden_field :prior_question_id, :value => #prior_question_id %>
</div>
<% end %>
<%= f.fields_for :answer do |answer| %>
<div class="form-group">
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text, :placeholder => "Enter your answer", :class => "form-control"%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% if #question.answer_type == 'Update' || #question.answer_type == 'Result' %>
<%= answer.hidden_field :answer_text, :value => "Updated" %>
<% end %>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<% end %>
<% if #question.answer_type == 'Update' || #question.answer_type == 'Result' %>
<div class="actions">
<%= f.submit "Done", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% else %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
Here's the code which does not work using form_tag:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
<%= ff.hidden_field :question_id, :value => #question.id %>
<tr>
<td><%= #question.question_text %></td>
<td>
<%= ff.fields_for :answer do |answer| %>
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% end %>
</td>
<td>
<%= ff.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</td>
</tr>
<% end %>
<% end %>
For completeness, the issue I'm having is that the generated html for the working code creates the following field:
textarea id="sale_qualifier_answer_attributes_answer_text" name="sale_qualifier[answer_attributes][answer_text]"
The broken code creates the following html:
Textarea id="sale_qualifier_answer_answer_text" name="sale_qualifier[answer][answer_text]"
So how can I get the html output to show "sale_qualifier[answer_attributes][answer_text]" rather than "sale_qualifier[answer][answer_text]" in this instance using form_tag?
In multiple nested forms the fields_for tag will be called against
right parent and attributes conventions should be followed right otherwise
it renders the form attributes wrong and results in errors.
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.fields_for :answer_attributes do |answer| %>
Above flow will be the right one & will generate attributes as they should be.
Hello guys I have array of hashes:
#buttons = [{:serno=>1, :parent_serno=>0, :name=>"Home"},
{:serno=>2, :parent_serno=>0, :name=>"Search"},
{:serno=>3, :parent_serno=>0, :name=>"Search Payment"},
{:serno=>4, :parent_serno=>1, :name=>"Problematic Search Payment"},
{:serno=>5, :parent_serno=>1, :name=>"Cash Error"},
{:serno=>6, :parent_serno=>2, :name=>"Payment Note"},
{:serno=>7, :parent_serno=>2, :name=>"Search Payment By Category"},
{:serno=>8, :parent_serno=>3, :name=>"Search Payment New"},
{:serno=>9, :parent_serno=>3, :name=>"User Mangement"}]
I want to create div for each array hash (for example hash x) that's "parent_serno == 0", write their name inside the div and create another div inside of it, for every array hash that's "parent_serno == hash x [:serno]".
I tried to create partial:
<% for m in #buttons %>
<% unless #parent.present? %>
<% if m[:parent_serno] == 0 %>
<div>
<%= m[:name] %>
<% #parent = m[:serno] %>
<% if #buttons.find{ |b| b[:parent_serno] == #parent }.present? %>
<% #buttons.find{ |b| b[:parent_serno] == #parent }.each do %>
<%= render partial: "navbar", object: #parent %>
<% end %>
<% end %>
</div>
<% end%>
<% else %>
<% if m[:parent_serno] == #parent %>
<div>
<%= m[:name] %>
<% #parent = m[:serno] %>
<% if #buttons.find{ |b| b[:parent_serno] == #parent }.present? %>
<% #buttons.find{ |b| b[:parent_serno] == #parent }.each do %>
<%= render partial: "navbar", object: #parent %>
<% end %>
<% end %>
</div>
<% end%>
<% end %>
<% end %>
and render it in a another html.erb document, but apparently it just doesn't work :(
It only gives me the name of the first hash in the array and recursion stops there I guess.
I think the partial you need should look something like this:
_button.html.erb
<div>
<%= button[:name] %>
<% render partial: 'button',
collection: #buttons.select { |b| b[:parent_serno] == button[:serno] } %>
</div>
index.html.erb
...
<% render partial: 'button',
collection: #buttons.select { |b| b[:parent_serno] == 0 } %>
...