Getting the latest entry for a user from a table - ruby-on-rails

I am having a table named names in which there is a column named name. The users will enter a text before proceeding to next page.
In the next page I want to show them the data they entered on previous page.
In controller I tried something like #names = current_user.names.all
In views I did
<ul class="names-list">
<% #names.each do |nname| %>
<li>
<%= link_to nname.name, edit_name_path(nname) %>
</li>
<% end %>
</ul>
and show the last list item for the latest date. But surely there will a Rails shortcut to fetch the last data for the user. Any help?
UPDATED : Now I did something like
#last_name = current_user.names.last in controller and in view
<%= #last_name.to_s %> and it is generates #<Name:0x007fae23ee1b68>
instead of name string.

I think <%= #last_name.to_s %> only outputs the object #last_name's name in memory, since thats specifically what you're asking it to output.
If your names table contains the column "name",
the correct code in your view should be <%= #last_name.name %>
This specifically outputs the object #last_name's name, and not the objects name itself (in memory).

Figured out via #Lanny
In controller
#last_name = current_user.names.last
In view
<%= #names.name %>

Related

Rails partial for collection, only display certain things for first element

I have a collection of elements I'm rendering in a partial, but I only want to display a certain element with the very first element. My specific instance is displaying email addresses but I only want the email icon to show once next to the first instance (similar to how the Android Contacts app does).
I have a very "hacky" solution that uses instance variables in the view, which is not a good practice. But I'm struggling to find a cleaner way to implement what I want.
The controller:
#email_addresses = EmailAddress.order(:primary) # primary is a boolean value
The partial:
# views/email_addresses/_email_address.html.erb
<div class="email-address">
<% unless #email_icon_displayed
<% #email_icon_displayed = true %>
<div class="email-address-icon">
<span class="icon email"></span>
</div>
<% end %>
<div class="email-address-value">
<%= email_address.value %>
</div>
</div>
Calling partial in view:
<%= render partial: "email_addresses/email_address", collection: #email_addresses %>
This works properly and only displays the email icon for the first element, but instance variables in the view seems like a bad idea.
This may be a little late, but I had the same goal and I achieved it by using a "hidden" counter variable in Rails partial collections.
I called my partial collection like this:
<%= render partial: "questions/possible_answer",
collection: question.possible_answers, as: 'value' %>
And inside my partial I can access the variable value_counter, which increments for each partial. So to run something with the first element only, I did it like this:
if value_counter == 0
#do something
end
I think your case you would need to access it like this: email_address_counter
Found this solution here: https://coderwall.com/p/t0no0g/render-partial-with-collection-has-hidden-index

Updating all with a dictionary in Rails

I have a small CMS-like program that has multiple pages that act like blog posts. Each page has content, and a position integer that identifies in what order they will appear on the page.
On my admin side, I have a draggable list of pages that I can reorder similar to how wordpress orders plugins. The page works as functions, and assigns the value of the dragged position to each page correctly. However, since all sortable pages have their own form, I cannot submit them all at once - only one at a time.
As an example, my code looks like this currently:
<div class="sortable">
<% #pages.each do |page| %>
<div class="dragBox">
<%= form_for(page) do |f| %>
<%= f.number_field :position, class: 'inPosition' %>
<% end %>
</div>
<% end %>
</div>
Because I can get every page_id tied to its new position, is it possible to submit those values in a new hash to get updated all at once in the controller? How would I go about doing this? Is there a better or easier way to do this? Thanks.

displaying one too many items rails

I am building a simple app and in many views I am displaying all of the objects associated with a certain model (many-to-one relationship). For example, I have a house model and an Item model where House has many Items. On the Show view for house I have the following code:
<% #house.items.each do |item| %>
<% if item.needed == true%>
<p>
<%= item.description %>
</p>
<% end %>
<% end %>
and this displays all the items along with one blank item. If I delete all the items, leaving an empty array there is still one empty item remaining. I can hack this using the code:
<% #house.items[0..-2].each do |item| %>
<% if item.needed == true%>
<p>
<%= item.description %>
</p>
<% end %>
<% end %>
This is probably a really simple question, but I would like to avoid using the latter code, and would like to understand why this is happening. Thanks.
The issue you are seeing is data related. This is to say, you need to figure out what is being returned by #house.items. Perhaps you have an item that has needed == true and a blank description? To trouble shoot this verify what is being returned by the house object in question by opening up the rails console, loading the house object in question and checking what is returned by house.items.
When using #house.items.new to set up a new Item object, it will alter the #house.items array, even though the new item is not yet persisted to the database. Example:
items = #house.items
items.length
# => 3
item = #house.items.new
items.length
# => 4
You could either add a check inside your loop for something like if item.persisted? or unless item.new_record?. Or, you could build the new item this way instead, which won't include it in #house.items until it's actually saved to the database:
item = Item.new(house_id: #house.id, needed: true)

How do I populate a dropdown list?

I want to populate values fetched from a database to a drop down list, using rails 3:
#details contains all the values from the database. While printing #details.inspect it displays in the web page as:
[<DeviceDetail DeviceID: 14448, No: 616">, <DeviceDetail DeviceID: 14448, No: 617">, <DeviceDetail DeviceID: 14448, No: 618">]........
In the loop I fetch the No details. How can I show No in a drop down list?
<% #details.each do |d| %>
<%=d.No%>
<% end %>
I added the following for displaying No in drop down list, but it's returning the error undefined method name for DeviceDetail:0x390f3f0. My database does not contain name filed, so what should I put in the :name field? The database table contains no, deviceid, speed and time.
<% #details.each do |d| %>
<%=collection_select(:device_detail, d.No, #details, :id, :name) %>
<% end %>
It this possible?
Is this part of a form? Use the select_tag, and select the input option for the same.
Check out http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag.
If I understand your question correctly, you will need to declare the instance variable #details in your Device controller. It should look something like this:
controller/device.rb:
def show
#details = Detail.all
end
Then, in your index and show pages, you can access that instance variable like this:
views/device/show.rb:
<% #details.each do |detail| %>
<%= render detail %>
<% end %>
It should loop through each of your details and render each one. If you only want to show the the "no" of details it should look like this:
<% #details.each do |detail| %>
<%= detail.no %>
<% end %>
Or, if you're on the show page you could simply call the detail for that page like this:
<%= detail.no %>
Is this what you were asking?

Rails - Easy way to display all fields in view

OK I'm sure I'm missing something here, but please forgive me I'm new to Rails.
Is there some way in Rails to display all the fields for an object rather than specifying each?
In my show.html template rather than going
<p>Name: <%=h #user.full_name %></p>
<p>Email: <%=h #user.email %></p>
I just want a oneliner to do this without having to type out each of the 15 or so fields I have.
Its an admin page so its fine if all the fields are shown (id, created_at, etc.)
If this was PHP it would take me about 5 secs using foreach, but I've googled (on the wrong things obviously) for an hour with no luck.
Thanks!
Something like
<% for attribute in #user.attributes.keys %>
<p><%= attribute.humanize %> <%= #user.attributes[attribute].to_s %></p>
<% end %>
could do the trick.
Matt
I suppose you want to display all attributes of a row from database table which is defined as ActiveRecord model. You can use class method column_names (every ActiveRecord model has it), which returns names of table columns in an array.
<%= User.column_names.collect { |col_name| "#{col_name.capitalize}: <p>#{#user[col_name]}</p>" }.join("\n") %>
<%= debug #user %>
simple way to show the object... that is what I usually use anyway!
#user.attributes.each{|key, value| puts "#{key} : #{value}"}
This is the snippet I used to blacklist some attributes I didn't want to show...
controller (user_controller.rb)
def show
keys_blacklist = %W(user_id name) #these are the fields to hide
#user_showlist = #user.attributes.except(*keys_blacklist)
end
view (show.html.erb):
<!-- language: ruby --><% for attribute in #user_showlist.keys %>
<b><%= attribute.humanize %></b>
<%= #user.attributes[attribute].to_s %>
<!-- language: ruby --><% end %>
You can also use instead:
#user_showlist = #user.attributes.slice(*keys_whitelist)
in order to display a whilelist of properties.
If you are using haml and want to loop through the attributes on for example a user object in a view:
- for attribute in #user.attributes.keys
%p
= attribute.humanize
= #user.attributes[attribute].to_s

Resources