I am trying to switch a for x in y loop to a each_with_index. First, I am trying to switch from for to each. The code before is
...
- row_bg_color_group = 'row_color_group_1'
- for link in #links
- construct_hyperlink(link.url_address, link.alt_text)
- if link.group.group_name != (current_group ||= '')
- display_group = current_group = link.group.group_name
- row_bg_color_group = rotate_rows_color_group
- else
- display_group = ''
%tr{:class => "#{row_bg_color_group}"}
%td
= link_to display_group, link.group, {"title" => link.group.group_description}
%td
%span#mainLink
= sanitize #address_url
%td= link.version_number
....
and rotate_rows_color_group is a helper in app/helpers. The code is
def rotate_rows_color_group
if session[:group_shading] == 'true' || params[:show]
cycle('row_color_group_1 color_group_1', 'row_color_group_2 color_group_2', 'row_color_group_3 color_group_3')
else
cycle('row_color_group_1', 'row_color_group_2', 'row_color_group_3')
end
end
The initial change I made is just to switch
- for link in #links
to be
- #links.each do |link|
This is changing my view from
to
The group name gets repeated. Is it a scope issue?
Since .each uses a block, any variable declared within it will be re-declared each time it is entered.
To avoid this, simply declare current_group outside the block:
...
- row_bg_color_group = 'row_color_group_1'
- current_group = ''
- #links.each do |link|
- construct_hyperlink(link.url_address, link.alt_text)
- if link.group.group_name != current_group
- display_group = current_group = link.group.group_name
- row_bg_color_group = rotate_rows_color_group
- else
- display_group = ''
%tr{:class => "#{row_bg_color_group}"}
%td
= link_to display_group, link.group, {"title" => link.group.group_description}
%td
%span#mainLink
= sanitize #address_url
%td= link.version_number
....
Related
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|
Here, I am building database driven radio buttons. I wanted to know if there is a way I can avoid eval and see checked is true or not !Thanks,
Index Controller
columns = Model.column_names
View in haml
- columns.each do |cols|
- check = "c[0].#{cols}==1? true : false" // checking value here
- negcheck = "c[0].#{cols}==0? true : false"// checking value here
- pluckid = "c[0].id"
- id = eval(pluckid)
%tbody
%td #{cols}
%td
= label_tag 'On'
= radio_button_tag("ABC",1,checked = eval(check), options = {})
= label_tag 'Off'
= radio_button_tag("xyz",0,checked = eval(negcheck), options = {})
You can use the [] method to access the value.
- columns.each do |cols|
- check = c[0][cols] == 1
- negcheck = c[0][cols] == 0
- id = c[0].id
%tbody
%td #{cols}
%td
= label_tag 'On'
= radio_button_tag("ABC", 1, checked = check, options = {})
= label_tag 'Off'
= radio_button_tag("xyz", 0, checked = negcheck, options = {})
eval("c[0].#{cols}==1? true : false")
can be written as
c[0].send(cols) == 1
I have loop that is checking if there is no image display a dummy image. I don't know what is wrong with this loop. It is not display the dummy image.Any help will be appreciated. Thank you
.col-md-12
- #applied_users.each do |user|
.applied
%a
%a.litle-modules.pull-left{href: show_user_public_path(user.id, ((Apply.where user_id: user.id, job_id: #job.id).first).id)}
.col-xs-6.col-md-5
.thumbnail
- if user.image != nil
= image_tag "profile_pic.jpeg"
- elsif user.image.nil?
= image_tag "profile_pic.jpeg"
.col-md-7.pull-right
%h2= user.fname
%p= user.location
%p= user.description
def show_private
#this method is responsible for showing the companies their job application page after matches have been made and after people have applied.
neo_results_array = []
percentage_match = []
#job = Job.find(params[:id])
#neo_results = Neo4jProcess.match_users(#job.id, 20000)
filtered_results = JSON.parse(#neo_results)
users = filtered_results["users"]
users.each do |user|
if user["score"] >= 0.005
neo_results_array << user["user_id"]
percentage_match << user["score"]
else
end
end
#final_array = neo_results_array.zip(percentage_match)
print #final_array
if #job.applies == [] && neo_results_array == []
redirect_to share_page_path(#job.id)
else #job.applies != [] || neo_results_array != []
applied = #job.applies.pluck(:user_id)
#applied_users = User.find(applied)
percentage_user_array = []
#final_array.each do |item|
percentage_user_array << item[0]
end
#matched_users = User.find(percentage_user_array)
end
end
just edited. here you can see my controller responsible for the img and other things
Remove condition from else
If you want to put some conditions for else as well use
elsif user.image.nil?
I currently have the following code:
- #alpha = Glossary.find(:all, :order =>"title ASC").group_by{|u| u.title[0]}
- #glossary = Glossary.find(:all, :order =>"title ASC")
- #alpha.each do|a|
%h1= a[0]
- #glossary.each do |g|
%p display stuff
This displays all of the glossary terms under each letter rather than only the ones that begin with the letter.. I've tried a few things but I'm not sure how to select the right thing.
You should be able to do everything with your #alpha instance variable, since you're using group_by:
- #alpha = Glossary.find(:all, :order =>"title ASC").group_by{|u| u.title[0]}
- #alpha.each do |alpha, glossary_array|
%h1= alpha
- glossary_array.each do |item|
%p= item
You're close. I think you just want to do
- #alpha = Glossary.order("title ASC").group_by{|u| u.title[0]}
- #alpha.each do |letter, items|
%h1= letter
- items.each do |item|
%p= item
So I have an application wide helper method for breadcrumbs, that I use in the layout, across all view pages.
Here is the method below
- if request.fullpath != '/'
.row
.col-md-12
#breadcrumbs
= link_to 'home', '/'
%span »
- #BreadcrumbsHelper = breadcrumbs
- i = 0
- #BreadcrumbsHelper.each do |name, breadcrumb|
= link_to name, breadcrumb
- if i != (#BreadcrumbsHelper.count - 1)
%span »
- i = i + 1
From what I understand, variables in the view should be instance variables and not methods, however declaring the instance variable in the view doesn't really seem to make sense, but I am not sure how else to go about it, would it be acceptable just to leave it as a method call ie breadcrumbs.each do for example? Would that even work? What is the best practice.
EDIT (the helper, just in case it helps) :
module BreadcrumbsHelper
def breadcrumbs
current_path = request.fullpath
noparam_path = current_path.split('?')
path_parts = noparam_path[0].split('/')
path_parts.shift
counter = path_parts.size
new_paths = Hash.new
counter.times do
new_path = ""
path_name = ""
i = 0
path_parts.each do |part|
if i < counter
if new_path == ""
new_path = '/' + part
else
new_path = new_path + '/' + part
end
path_name = part
i = i + 1
end
end
counter = counter -1
#path functions
def routeValid(new_path)
route = "/" + new_path.gsub("_","/")
route_valid = true
begin
Rails.application.routes.recognize_path(route, :method => :get)
rescue
# error means that your route is not valid, so do something to remember that here
route_valid = false
end
return route_valid
end
def processPath(new_path, path_name, new_paths)
if routeValid(new_path) == true
#Optional Overrides
if path_name == "signup"
path_name = "sign up"
end
new_paths[path_name] = new_path
end
end
processPath(new_path, path_name, new_paths)
end
new_paths = Hash[new_paths.to_a.reverse]
return new_paths
end
end
Helpers are modules that are included in the view. They are not accessed via instance variables.
You should be able to access a method defined in a helper directly in a view. So rather than writing
- #BreadcrumbsHelper = breadcrumbs
- i = 0
- #BreadcrumbsHelper.each do |name, breadcrumb|
you would just write something like
- breadcrumbs.each do |name, breadcrumb|
You will also probably want to capture the count of breadcrumbs before the loop, with something like
- my_breadcrumbs = breadcrumbs
- breadcrumbs_count = my_breadcrumbs.size
- my_breadcrumbs.each do |name, breadcrumb|
and replacing
#BreadcrumbsHelper.count
with
breadcrumbs_count