Update object array values in rails view - ruby-on-rails

I have #table_name(object array) contain many objects.
I want to replace its column values one by one with my values..
i have use
<% for group_permission in #table_name %>
<%end%>
How i change Please help me

Hi If #table_name is a collection of objects, you can get each as
<%#table_name.each do |t|%>
<%= t.group_permissions %>
<%end%>
You also can get attributes as
<% t.attributes.each do |a| %>
<%.......%>
<%end%>
Values and names of attributes as
a[0]
a[1]

Related

How to count associated records in rails and display as an integer.

I have several items, each item also has several sub items.
I would like to strictly count the number of sub items attached to each item and display that as an integer. Is there a way to do this?
I tried this, but it does not seem to return correctly.
<div class="items" id="cell"><%= sub_item.count %></div>
Something like this should work, can you add your objects structure?
<% #items.each do |item| %>
<div>Count: <%= item.subitems.count %></div>
<% end %>
I am assuming you have a #items instance variable in your view, and for each item in items you have several item.subitems, so you could do something like this:
#items.each do |item|
<div class="items" id="cell">
<%= item.subitems.count %>
</div>
item.subitems.each do |subitem|
// Do stuff
end
end
Of course i could've assumed wrong.

Rails Loop Through Hash

A really simple question but I just can't seem to get it working.
There is only one Property included below but there could be more than one within Properties. How can I iterate through this hash and display just the Name of each Property?
{"GetPropertiesResponse"=>{"Properties"=>{"Property"=>{"Breakfast"=>"IN", "Country"=>"GB", "Currency"=>"GBP", "Id"=>"1834", "Name"=>"Hotel Name"}}}}
I've tried this in my view:
<% #json['GetPropertiesResponse']['Properties']['Property'].each do |property| %>
<%= property['Name'] %>
<% end %>
I'm getting this error:
no implicit conversion of String into Integer
If you are saying there might be more than one property hash then this should work:
<% #json['GetPropertiesResponse']['Properties'].each do |property, value| %>
<%= value['Name'] %>
<% end %>
I would use #each_value on this hash since you don't appear to be using the keys
<% #json['GetPropertiesResponse']['Properties'].each_value do |value| %>
<%= value['Name'] %>
<% end %>
Should work. Note that the second line is <%= value['Name'] %> and not <%= property['Name'] %>
P.S. On a different note, I don't know why you're using the key "Property" inside of your Properties hash. That just seems like a good way to confuse yourself in exactly this way. Your keys within the Properties hash should be something unique to the property they describe. Since each will be a property, the string "Property" doesn't help describe or differentiate.

get specific values from rails array

I think I have a correct array going for rails?
#lv = {'apple' => ['tags', 'red'], 'name' => ['more tags', 'taggers']}
I was wondering how I can display certain parts through a loop. For instance, how would I only display apple and name?
<% #lv.each do |me| %>
<%= me %>
<% end %>
This just displays the whole #lv message, and doesn't only display apple and name. And then I'd like to be able to get only the tagged values of specific ones, so say if I need to get tagged value of apple, it should only display tags and red How do I do this with rails?
Thanks!
Your #lv variable is a hash, so using .each will only give you a combined key-value pair as the block parameter (that's what me ends up being). Instead, use each_pair; that way you can get separated variables for the keys and the values. Like so:
<% #lv.each_pair do |key, value| %>
<%= key %>
<% end %>
Edit
This is in response to your comment in the question as well. The key will end up being just the apple, or name, part of your hash. The value parameter is whatever is pointed to by the key, which in this case is the actual array of items (which I think is what you're calling tags). For example, your hash contains two key-value pairs, and as we iterate over them, in the first loop key = apple, and value=['tags', 'red']. To output that array of values, you could do it a couple of different ways:
Loop over the tag array
<% #lv.each_pair do |key, value| %>
<%= key %>
<%= value.each do |tag| %>
<%= tag %>
<%= end %>
<% end %>
As a comma separated string:
....looping code
<%= value.join(", ") %>
Or just spit it out as-is in array notation:
....looping code
<%= value %>
Or if you just wanted a specific element in the value array, then yes you can just do value[0], or value[1]...etc.
Let me know whether that is not what you are asking.

Loop over Object attributes in Ruby on Rails

Is it possible to loop over an object's attributes in Rails? I have an object and rather than code up each attribute in the view, I want to output each of them in the view as there are quite a few.
I have an object called #work_profile which has many attributes, mainly boolean check box values.
Edit: I see I can use #work_profile.attributes. Any help on formatting the hash into something more user friendly would be great.
The ActiveRecord::Base.attributes() method returns a hash of all the attributes. You can use that to loop over all attributes.
#work_profile.attributes.each do |attr_name, attr_value|
...
end
In a view, this would give:
<% #work_profile.attributes.each do |attr_name, attr_value| %>
<div class="work_profile_attribute">
<%= attr_name %>: <%= attr_value %>
</div>
<% end %>

Rails 'params' variable

In reference to this
I've created a question in a webform like this:
<div class="form_row">
<label for="features[]">Features:</label>
<% [ 'scenarios', 'role_profiles', 'private_messages', 'polls' ].each do |feature| %>
<br><%= check_box_tag 'features[]', feature,
(params[:features] || {}).include?(feature) %>
<% end %>
</div>
So if scenarios and private_messages gets checked and I print out params[:features] I will get:
scenariosprivate_messages
I was wondering how would I be able to obtain scenarios and private_messages separately from params. Is the mapping params[:features] = "scenariosprivate_messages" or is it really params[features] = ["scenarios", "private_messages"] ? If it's the latter how can I loop through them?
I write in my view:
<%= params[:features].each {|param|
param.capitalize
} %>
and I still just get scenariosprivate_messages printed.
Try this instead:
<% params[:features].each do |param| %>
<%= param.capitalize %>
<% end %>
The problem with your original solution is that you're printing out the result of the block, which is the array itself, rather than printing out each element of the array.
You shouldn't be using params in your views. You're best off assigning params[:features] to an instance variable in your controller and then iterating over that in your view.
But to answer your question, you're putting the equals sign for output in the wrong place. You want to output each element of the array individually instead of outputting the result of the loop.
You must use humanize:
<% params[:features].each do |param| %>
<%= param.humanize %>
<% end %>
According to this blog post you should be able to access them individually as params[:features]['scenarios'] etc. Looping should just work like with all other arrays -- eg
params[:features].each { |param|
# do something with param
}

Resources