I'm trying to display these records in a table with the top level records first and all children and grandchildren under them with a '-' , '--' etc. I'm having trouble figuring out how to do it programmatically though, without just repeating myself. This is what I have that displays the the top level and first child of each.
- #staff_resources.top_level.each do |staff_resource|
= render 'staff_resource', staff_resource: staff_resource, depth: 0
- staff_resource.children.each do |staff_resource_child|
- depth = 1
= render 'staff_resource', staff_resource: staff_resource_child, depth: depth
%td= "#{'-' * depth} #{staff_resource.name}"
%td.icons.text-center.nowrap
= link_to edit_admin_staff_resource_path(staff_resource), class: 'edit-icon' do
= image_tag 'common/edit.svg'
= link_to admin_staff_resource_path(staff_resource), method: :delete, data: { confirm: "Are you sure?" }, class: 'delete-icon' do
= image_tag 'common/delete.svg'```
Related
In my form I have :option_ids which passes a list of IDs back to the model to build associations with the applicable Options. In my case there are groups of options allowing the user to select any combination of options.
Currently I have this in my form:
- #option_groups.each do |group|
- unless group.options.blank?
.form
h3 = group.name
.checkboxes
= line_item.collection_check_boxes :option_ids, group.options, :id, :name_and_price do |option|
.checkbox.horizontal
= option.check_box(class: "check")
= image_tag option.object.photo.variant(resize: "65x65") if option.object.photo.attached?
= option.label
Recently we added a condition to the OptionGroup model allowing users to define whether multiple options from this OptionGroup could be selected(checkboxes), or only one(radio buttons).
What I'm wondering is if there's a way I can still populate the :option_ids parameter using a combination of checkboxes and radio buttons. I've tried the following but it messes up the params hash
-#option_groups.each do |group|
- unless group.options.blank?
.form
h3 = group.name
- if group.multiple?
.checkboxes
= line_item.collection_check_boxes :option_ids, group.options, :id, :name_and_price do |option|
.checkbox.horizontal
= option.check_box
= image_tag option.object.photo.variant(resize: "65x65") if option.object.photo.attached?
= option.label
- else
span Please select one
.checkboxes
= line_item.collection_radio_buttons :option_ids, group.options, :id, :name_and_price do |option|
.checkbox.horizontal
= option.radio_button
= image_tag option.object.photo.variant(resize: "65x65") if option.object.photo.attached?
= option.label
Has anyone encountered a scenario like this before? How would someone conditionally impose exclusivity while not messing up the other id's in the form attribute?
I am using ajax with rails to get single random row I want to display just 10 rows with every request by ajax get one single random and display it, i try something like that, Model.all.sample or Using offset with first but my problem is duplication how can avoid it or how can i set all response to check if I sent it before or not Note: I send all elements was appended as array for backend to check if i send it before and change it but i have wrong
result
my code is :- in backend is my function
def get_10
arr = params['arr']
if arr.nil?
#rand_record = Phrase.all.sample
else
i = 0
if i < 10
#rand_record = Phrase.all.sample
while(i < arr.length) && (i<10)
flag = arr[i].include?#rand_record.name
if flag
#rand_record = Phrase.all.sample
i = 0
elsif flag == false
return #rand_record
end
i+=1
end
end
end
respond_to do |format|
format.js { }
end
end
in my js ajax is :
function myFunction(){
var arr = []
var len = $('li').length
for (let i=0 ; i< len; i++){
var attr = $('li')[i].childNodes['0'].data
arr.push(attr)
}
$.ajax({
method: 'GET',
url: '/phrase',
dataType: 'script',
data: {arr: arr}
}).done(function(data){
console.log(data);
})
}
in template is:
<div class="form-group">
<%= button_tag 'GetPhrases', type: 'button', onclick:"myFunction()", class: 'btn btn-default' , id:"get" %>
</div>
my result is
this pharase number 1
this pharase number 5
this pharase number 4
this pharase number 5
this pharase number 8
enter image description here
I want to avoid duplication I want to retrive just 10 random single row without duplication
Be careful with sample on the active record relation, instead do:
Model.where(id: Model.all.pluck(:id).sample(10))
This will just pluck all the IDs and sample 10 of them and then select records with those random 10 ids.
Using Rails 3/Ruby 1.9.3, I have to dynamically generate a form using an array of values. The form generates properly with the exception that the #sub_fields array is being output to the screen between the form values and the submit button.
The HAML code that generates the form looks like this:
= form_tag "/magazine/subscribers" do
= #sub_fields.each do |k,v|
.formField
- if v.has_key? :evaluate
= label_tag k.to_s, v[:label_text]
= v[:evaluate].call(k)
- else
- unless v[:input_type] == :hidden_field
= label_tag k, v[:label_text]
- if v[:select_options]
= select_tag(k, options_for_select(v[:select_options].call))
- else
= eval(v[:input_type].to_s + "_tag '#{v[:value].to_s}'")
- if v.has_key? :tooltip
.fieldTip
%ul
- v[:tooltip].each do |tip|
%li= tip
.formAction
= submit_tag "localize edit"
Use - instead of =
- #sub_fields.each do |k,v|
I have created a loop, to calculate a total rating of a record. To do this I am first looping through all the child records (ratings), extracting the rating from each row, adding it to the total and then outputting the total.
<% total = 0 %>
<% for ratings in #post.ratings %>
<% total = (total + ratings.rating) %>
<% end %>
<%= total %>
My question is, simply, Is this the rails way?
It achieves the desired result, although needs 5 lines to do so. I am worried I am bring old habits from other languages into my rails project, and I am hoping someone could clarify if there is an easier way.
The following, preferably in the controller, will do it succinctly:
#rating = #post.ratings.sum { &:rating }
If that seems cryptic, you might prefer
#rating = #post.ratings.inject(0) { |sum, p| sum + p.rating }
Note, however, that this will fail if any of the ratings are null, so you might want:
#rating = #post.ratings.inject(0) { |sum, p| sum + (p.rating || 0) }
You should generally keep logic out of your views. I would put that code in a helper or a controller, and the call a method to calculate the total
Put the following in your controller, then you just need to use #rating in your view:
total = 0
#rating = #post.ratings.each { |r| total += r.rating }
Or you could move it into the Post model and do something like:
def self.total_rating
total = 0
ratings.each { |r| total += r.rating }
total
end
and then simply call #post.total_rating
I'm new to Ruby on Rails. How can I display products in two columns?
When I write the following, the right column will display the same products, but I want to display
some in the left and some in the right columns.
#main_container
.left_col
%div{"data-hook" => "___homepage_featured_products"}
%h3
Featured Activities
- #featured.each do |pr|
- #product = pr
%a.box{:href=>url_for(#product), :title=>"#{#product.name} | #{#product.location}"}
- if #product.images[0]
.img{:style=>"background-image: url('#{#product.images[0].attachment.url(:original)}')"}
.details
%h3
= #product.name.truncate 20
%p.infos
= image_tag #product.activity_type.icon, :class=>"pictogram" rescue ''
%span= #product.activity_type.name.titleize rescue ''
\/
%span.price= number_to_currency #product.price rescue ''
\/
= #product.location
\/
= #product.level
%p
= #product.description.truncate(120) rescue ''
.right_col
You could put each product into its own div, and then use CSS to float them to the left so that a maximum of 2 boxes will appear next to each other horizontally. This will give the effect of a 2 column layout. As an example:
#main_container { width: 900px; }
.featured_product { width: 450px; float: left; }
Add padding etc as needed.
Alternatively you could split the array after you retrieve it from the database and run the code twice, once in the left column and once in the right:
#left, #right = #featured.in_groups_of((#featured.count / 2.0).ceil, false)