Block in rails 3 - ruby-on-rails

My view:
<%= 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>
I'm trying to move the <% end %> from the Current Rating block to the end so that I can call the rating_phin variable in my text_field_tag, however when I move the <% end %> to the end of my view, everything after the Current Rating block is not being rendered. What am I doing wrong? How can I update the rating_phin.notes instead of rec.notes in my text_field_tag at the end of my view?

That rating_phin variable only exists within the scope of that each loop.
If you require it later, you will have to loop again:
<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>
I'm not sure why you would expect that moving the <% end %> tag would be the right thing to do.
If you're getting lost inside of ERB, which is not especially difficult considering how fussy and convoluted HTML can get, especially when there's live Ruby in it, you might want to try HAML. It would reduce this down to about half the number of lines and remove a lot of the <% %> and closing tag debris that tends to make ERB hard to read.

Related

Rails - Kaminari - paginate_array returns duplicate records

I'm trying to paginate the following array. At the moment the table is being appended to, but only with the same records as the first page:
def collection
#collection ||= begin
viewable_pupils.where(level: level, group_id: group_id).ordered
.map{ |p| Schools::Admin::PupilReportTableFacade.new(p, school.calendar_range_for_year_starting(report_start_year)) }
end
#collection = Kaminari.paginate_array(#collection).page(params[:page]).per(10)
end
helper_method :collection
My view is as follows:
<div class="container">
<%= render 'schools/admin/reporting/collection_partial' %>
</div>
<%= link_to 'Load More', school_reporting_load_more_path, remote: true, id: "load_more_link", class: 'btn btn-info' %>
And the partial being rendered:
<% collection.each do |pupil| %>
<div class="well well--bordered well--skinny well--collapse-pad well--shadow">
<div class="marking-header marking-header--table marking-header--table-even">
<div class="marking-header__cell">
<%= pupil.first_name %>
</div>
<div class="marking-header__cell"><%= pupil.last_name %></div>
<div class="marking-header__cell" align="center">
<% pupil.histories.each do |history| %>
<% next unless school.calendar_range_for_year_starting(report_start_year).cover?(history[:created_at]) %>
<%= history[:year] %> <%= history[:level] %>
<%= history[:created_at] %>
<% end %>
</div>
<div class="marking-header__cell">
<%= "Total completed: #{pupil.pupil.activities_completed_year_starting(report_start_year)}" %></br>
<%= "(#{pupil.pupil.comprehensions_completed_year_starting(report_start_year)} x comprehensions)" unless pupil.pupil.comprehensions_completed_year_starting(report_start_year).zero? %></br>
<%= "(#{pupil.pupil.crosswords_completed_year_starting(report_start_year)} x crosswords)" unless pupil.pupil.crosswords_completed_year_starting(report_start_year).zero? %></br>
<%= "(#{pupil.pupil.debates_completed_year_starting(report_start_year)} x debates" unless pupil.pupil.debates_completed_year_starting(report_start_year).zero? %></br>
<%= "(#{pupil.pupil.word_definitions_completed_year_starting(report_start_year)} x word definitions" unless pupil.pupil.word_definitions_completed_year_starting(report_start_year).zero? %>
<div class="gems">
<% pupil.milestone_badges.each do |badge| %>
<%= image_tag image_path(badge.image), class: 'gem' %>
<% end %>
</div>
</div>
<div class="marking-header__cell">
<input type="checkbox" name="pupils[]" value="<%= pupil.id %>" class="tick-check" id="check-<%= pupil.id %>" data-behaviour="update-compare-count">
<label for="check-<%= pupil.id %>"></label>
<%= "Total this year: #{pupil.pupil.total_points_this_year}" %></br>
<%= "(Total all time: #{pupil.pupil.total_points})" %></br></br>
<%= "iHub points: #{pupil.pupil.score}" %></br>
<%= "Teacher Points: #{pupil.pupil.total_points_year_starting(report_start_year)}" %></br>
<%= "Words Read: #{pupil.pupil.words_read_year_starting(report_start_year)}" %></br>
</div>
</div>
</div>
<% end %>
Finally my ajax:
$('.container').append("<%= escape_javascript(render 'schools/admin/reporting/collection_partial')%>");
$('#load_more_link').replaceWith("<%= escape_javascript(link_to 'Load More', school_reporting_load_more_path, remote: true, id: "load_more_link", class: 'btn btn-info')%>");
If any more info is needed please ask, I feel I've tried everything I can think of at this point.

Responsive checkbox is too wide

I am trying to do simple quiz using Rails and Bootstrap, but I have problem with checkboxes being too wide on small screens and it covers label of that checkbox (.
My view looks like this:
<div class="center jumbotron">
<h2><%= t(:quiz) %></h2>
<div>
<%= form_for(:test, url: quiz_path) do |f| %>
<% #quiz.tasks.each_with_index do |task, index| %>
<div>
<% if task = Task.find_by(id: task) %>
<%= image_tag(task.asset, class: "img-responsive") %>
<%= task.text %>
<% answers = task.correct_answers + task.wrong_answers %>
<% answers.shuffle! %>
<fieldset>
<!-- Dodać półotwarte -->
<% answers.each do |answer| %>
<div class="checkbox">
<%= f.check_box("task#{index}", {class: "checkbox", multiple: true}, answer, nil)%>
<%= f.label "task#{index}", answer %>
</div>
<% end %>
</fieldset>
<% end %>
</div>
<% end %>
<%= f.submit t(:finish_quiz), class: "btn btn-lg btn-primary" %>
<% end %>
</div>
</div>
How do I make it look normal a.k.a. like desktop version, just bigger (square-like of course)?

Rails Same page with url paramaters only change

Actually this is my view am getting values from mysql database and i showed in browser based on the params page=1 page =2 page =3 and i want to change the params[:page] in form next button click.
Based on pages=1,2,3 questions and answers will vary thats why i need url to be change while button click in same page.! like below URL http://localhost:3000/responses/new?page=1 Thanks in advance.
<%= form_for (#response) do |f| %>
<% #questions.each do |pgquestion| %>
<% if pgquestion.group_id == 0 %>
<%= label :pgquest,pgquestion.description %><br><br>
<% else %>
<%= label :pgquest,pgquestion.description %>
<% (1..pgquestion.question_value.to_i).each do |i| %>
<%= radio_button_tag "ans_value[#{ pgquestion.quest_id }]", i %>
<% end %>
<br><br>
<% if pgquestion.question_type == "textarea" %>
<%= text_area "", "ans_value[#{ pgquestion.quest_id }]" ,size: '80x5' %><br><br>
<% else if pgquestion.question_type == "text" %>
<%= text_field "", "ans_value[#{ pgquestion.quest_id }]" %> <br><br>
<% end %>
<% end %>
<% end %>
<% end %>
<div style="text-align: center">
<%= f.submit 'Prev' ,:class => 'btn btn-primary',:name => "previous_button" %>
<%= f.submit 'Next', :class => 'btn btn-primary' %> <br><br>
<p><%= f.submit "Continue" %></p>
<p><%= f.submit "Back", :name => "previous_button" %></p>
</div>
<% end %>
As far as i understand your problem use the will_paginate gem https://github.com/mislav/will_paginate

Can't get shared filters in home view to work in spree demo

In the spree demo (latest version), I tried to use shared/filters instead of shared/taxonomies in views/spree/home/index.html.erb:
<% content_for :sidebar do %>
<div data-hook="homepage_sidebar_navigation">
<%#= render :partial => 'spree/shared/taxonomies' %>
<%= render :partial => 'spree/shared/filters' %>
</div>
<% end %>
I get the filters (All Taxons), but when I pick some, like Bags and T-Shirts and click on search, the filter doesn't work.
I didn't change anything in shared/filters:
<% filters = #taxon ? #taxon.applicable_filters : [Spree::Core::ProductFilters.all_taxons] %>
<% unless filters.empty? %>
<%= form_tag '', :method => :get, :id => 'sidebar_products_search' do %>
<%= hidden_field_tag 'per_page', params[:per_page] %>
<% filters.each do |filter| %>
<% labels = filter[:labels] || filter[:conds].map {|m,c| [m,m]} %>
<% next if labels.empty? %>
<div class="navigation" data-hook="navigation">
<h6 class="filter-title"> <%= filter[:name] %> </h6>
<ul class="filter_choices">
<% labels.each do |nm,val| %>
<% label = "#{filter[:name]}_#{nm}".gsub(/\s+/,'_') %>
<li class="nowrap">
<input type="checkbox"
id="<%= label %>"
name="search[<%= filter[:scope].to_s %>][]"
value="<%= val %>"
<%= params[:search] && params[:search][filter[:scope]] && params[:search][filter[:scope]].include?(val.to_s) ? "checked" : "" %> />
<label class="nowrap" for="<%= label %>"> <%= nm %> </label>
</li>
<% end %>
</ul>
</div>
<% end %>
<%= submit_tag Spree.t(:search), :name => nil %>
<% end %>
<% end %>
Any ideas why?

Having a Variable in a Render get transfered to the HTML

Sorry for the confusing title, but I'm not really sure what the exact problem is because I don't know Ruby all that well. Anyway, on to the problem!
So I have a render:
<div class="form" id="dept_div">
<span class="label search_label">Department:</span>
<% query = "<option></option>" %>
<% Department.all.each do |d| %>
<% query << "<option>"+d.dept+"</option>" %>
<% end %>
<%=select_tag :major, query.html_safe, class: "search_tag", id: "dept_drop" %>
</div>
That I try and plug in over here:
<span class="su_label">
<%= f.label :major %>
</span>
<% if !in_mobile_view? %>
<div class = "su_textfield">
<div class="checkbox">
<% else %>
<div data-role="fieldcontain" class="field_group">
<fieldset data-role="controlgroup" data-type="horizontal">
<% end %>
<!-- check privacy preferences to decide whether box should already be checked -->
<% if !hasnoprefs && #user.privacy_prefs.include?("nomajor") %>
<%= check_box_tag "major", 1, true %>
<% else %>
<%= check_box_tag "major" %>
<% end %>
<%= label_tag 'major', 'Hide' %>
<% if !in_mobile_view? %>
</div>
<%= render 'courses/form_partials/majors' %>
</div>
<% else %>
<%= f.text_field :major, class: "settings_inputfield" %>
</fieldset>
What I want is the variable major (which in the class User) to become the item selected from the render (which is a drop down menu). Any ideas?
Try...
<%= f.collection_select :major, query, :id, :major, {:include_blank => 'None' } %>

Resources