kala-tamin Rails - Easy way to display all fields in view


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!




Solutions for tracking cucumber features and scenarios? [closed]

1:



Framework design patterns
Something like.
Unpacked gem in vendor/gems not in a versioned directory
<% for attribute in @user.attributes.keys %>   <p><%= attribute.humanize %> <%= @user.attributes[attribute].to_s %></p> <% end %> 
could do the trick..
Open source Rails projects with great RSpec usage
Matt.
Looking for a github project that represents Rails best practices [closed]


Is there a way of doing filtering joined associations using named scope?


Making a has_many/belongs_to association immutable

2:



Best logic for storing categories of images
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") %> 


3:


<%= debug @user %> 
simple way to show the object...

that is what I usually use anyway!.


4:


@user.attributes.each{|key, value| puts "#{key} : #{value}"} 


5:


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..


6:


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 



85 out of 100 based on 65 user ratings 565 reviews