Rails/Sqlite 3 - Displaying table data on page - mysql

I'm very very very new to Rails, and I've got a fast approaching deadline to code my first rail app as part of a challenge.
I've so far created the basic blog application, to get my head around how rails works.
I need to display a list of all data held within a table on a page in my site. In blog, I've got an index page - which lists all blog posts so I've been trying to follow the same process.
I've created a populated table in my database, and a controller - but when I try and display info from that database on my page I get the following error:
uninitialized constant RecordsController::Aliens
class RecordsController < ApplicationController
def index
#records= Records.all
end
end
All I want to achieve is a simple table, which has the list of my records in, so far I'm not having much luck retrieving the data from the database.
Any help appreciated!

I assume you want to list all aliens am I right?
As your model is called Alien you can call it like that:
def index
#aliens = Alien.all
end
And then in your view something like:
<% #aliens.each do |alien| %>
<div class="alien">
<ul>
<% alien.attributes.each do |attr_name, attr_value| %>
<li><strong><%= attr_name %>:</strong> <%= attr_value %></li>
<% end %>
</ul>
</div>
<hr>
<% end %>

Related

Hide a Post independently if current_user has marked Post as 'hidden'

I've been at this for days now without any luck. but I'm trying to allow users to hide or mark Posts as read independently. But I simply cannot get it to work. Maybe it's because my brain is currently inside-out.
Here's my query:
<%
#posts.includes(:postsettings).where.not(
postsettings: {user_id: current_user.id}).where(postsettings:
{delete_post: true}).each do |post|
%>
What I'm trying to do here is Exclude an entire Post IF any of the postsettings includes the current_user.id – But as soon as another user also marks it as deleted – It pops back up again for current_user because the criteria is met reversibly.
Everything else is set up correctly to the best of my knowledge, using a join table.
Any help would be greatly appreciated. What would be the correct approach?
I think this should work, I dont get why you're using two where queries on for relating same object.
<% #posts.includes(:postsettings).where.not(
postsettings: {user_id: current_user.id, delete_post: true}).each do |post| %>
.....
<% end %>
Hope this helps

How to show all the Users who have liked my specific Items

I apologize for the newbie questions still relatively new to rails. I'm trying to show all the users who have liked a current user's specific items. With the help of the SO community and looking at different Rails guides - I'm 85% there. Currently, I'm displaying all users who have liked all items (not just my specific ones which is what I want) I've listed below all the relevant simple code - thank you so much guys!!
Index.html.erb
<%- #likers.each do |liker| %>
<%= image_tag liker.avatar, width: 25, class: "css-style" %>&nbsp
<%= liker.username %>
<% end %>
Items_controller
def index
#items = Item.order("created_at DESC")
if current_user.present?
#likers = Item.where("user_id", current_user.id).map(&:users).flatten
end
end
So, you want all #likers to be all the people who like the current_user's items?
#likers = current_user.items.map(&:likes).flatten.map(&:user).flatten.uniq
I've added the uniq so if a user likes more than one of these posts, you won't see them turn up multiple times. You can omit that, of course, if you want duplicates.
I'm also making some guesses about your other models based on your previous question, so you might need to tweak it depending on your actual implementation.

Customizing a table cell<td> on my index.htm.erb view using rails 3

My index.html.erb page under views/projects displays 5 fields(cells) under my projects tab.
Product, Status, POCs, est_date and delivery_date. I am using the following code in my table:
<% #projects.each do |project| %>
<tr>
<td><%= link_to project.product, project %> </td>
<td><%= project.status %><%= link_to '...MORE', project %></td>
<td><%= project.pocs %></td>
....
....
So basically the table/view lists all of my projects and you can click on any of the projects to view a detailed view (show.html.erb) of all the fields in each project.
The Status field/cell can get rather large with all the details that can be entered into it.
My question is this. How can I adjust the Status cell in the index view to only show the first 5 lines of text. If more then 5 lines show my link (...MORE). When clicked, go to the details view (show.html.erb) where you can view the the details.
I just don't want the index view getting too large and full of too much text. It should just be a quick summary.
Use the truncate helper like this:
truncate("line1\nline2\nline3\nline4", length: 2, separator: "\n")
will output:
"line1\nline2\nline3..."
The last example on the apidock page shows how you can pass a block:
truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }
So, it seems the block only works in rails 4. If you have rails 3 you might just want to pass an empty string as the omision then add your link after it.

Select Box not filling properly in rails

I am creating a select box for a form using this in _form.html.erb
<%= f.select(:category_id,options_for_select(#cats)) %>
#cats is an array created in my controller like this:
#cats = []
categories.each do |c|
#cats.push([c.full_name,c.id])
end
The select box is properly filled, and the selected foreign key is even properly saved to the database. The problem is, when I come back in my edit action, the select box is moved back to the first item in the list, not the one corresponding to category_id. Reading the documentation it seems like this should just magically work. How do I get it to select the proper value?
When you use the select helper you just pass the choices not the full option tags like you would with the select_tag helper. Try this instead
<%= f.select(:category_id, #cats) %>

Reconstruction User based Modular Website

I am developing a website in Ruby on Rails, MySQL, and Javascript.
The website is modular, meaning that a user can customize their homepage and drag and drop one module to another area where it will live. there are three columns where the module can be dropped. (Think iGoogle or Netvibes)
What is the best way to store this data, so that when the user returns their homepage can be reconstructed quickly?
I've considered doing it in a way where each module get's an ID that corresponds to the user, it's row and it's positiong in that row. (So something like.. user|column|row, would equal 1204|3|27, meaning that for user #1204 this module would be in column #3 and 27 spaces from the top.
Then when the user returns, just loop through, adding 1 to each until it reaches the end of the column and start again at the other one until all 3 columns are populated.
I feel like this would be very slow though, and there must be a better way to do it.
Any suggestions? Mostly looking for Database structure, but if you have some Rails code that would corrilate to this I wouldn't mind seein git.
I think the best is to keep your data in a single text field
For example 1204|3|27 should be in a text field ... with a good index per user id you should get the configuration very very fast. After that you just need to "explode" your configuration for "|" .
Regards
I say model it very straightforwardly:
class ModuleInstallation < AR::Base
belongs_to :user
belongs_to :module
validates_presence_of :column
validates_presence_of :row
end
class User < AR::Base
has_many :module_installations, :order => :column
has_many :modules, :through => :module_installations
end
Then let your controller handle any more sorting, something like this (untested, but look through it and the documentation for the methods I'm using to get the concepts):
#columns = current_user.module_installations.group_by(&:column)
#columns.each { |column, modules| modules.sort! { |x,y| x.row <=> y.row } }
Then your view is relatively simple:
<% #columns.each do |column, modules| %>
<div class="column-<%= column %>">
<% modules.each do |module| %>
<div class="module">
<%= module.to_html %>
</div>
<% end %>
</div>
<% end %>