I am building a rails dictionary application. I have defined and created my whole dictionary in a simple table that draws from a .txt document via a buffer where the view looks like this:
<% #words.each do |word| %>
<tr>
<td><%= word.word %></td>
<td><%= word.wordtype %></td>
<td><%= word.description %></td>
<td><%= link_to 'Show', word %></td>
</tr>
<% end %>
Controller:
def index
#words = Word.all
end
How do I go about adding an alphabetical index at the top of the page that will display only the words for that letter? I am new to rails and ruby so any help is appreciated.
The alphabetical_paginate gem helps you implement this.
If you wish to not use a gem, this answer has some tips that you can borrow.
Related
I am trying to put in a link_to on my table to go to the show action but it is putting the URL as /admin/vulnerabilities.object_id instead of /admin/vulnerabilities/object_id
my index view is:
...
<% #vulnerabilities.each do |vulnerability| %>
<tr>
<td><%=link_to vulnerability.id, admin_vulnerabilities_path(vulnerability) %></td>
<td><%= vulnerability.type %></td>
<td><%=h truncate(vulnerability.description, :length => 80) %></td>
<td><%= vulnerability.published %></td>
<td><%= vulnerability.modified %></td>
<td><%= link_to vulnerability.href, vulnerability.href , target: :_blank %></td>
</tr>
<% end %>
...
I have a show.html.erb template setup and my show action is as follows:
def show
#vulnerabilities = Vulnerability.find(params[:id])
end
From what I can see, this should work but when clicking the links it just redirects to the index page, effectively refreshing it and not using my show page at all.
It would be helpful if you added the relevant parts of your routes.rb file to your question, but I speculate that the problem is that admin_vulnerabilities_path(vulnerability) should be admin_vulnerability_path(vulnerability).
Also, as noted in the comments, it is probably better to use #vulnerability as your instance name since find will return a single record.
My applications currently uses the Ancestry gem to create a navigation tree.
How do I display the name of a page which is the parent of another in the index view?
i.e. currently I do...
<% #pages.each do |page| %>
<tr>
<td><%= link_to page.name, edit_page_path(page) %></td>
<td><%= page.ancestry %></td>
</tr>
<% end %>
I want the page.ancestry to be the parent's name, not the ID.
<td><%= page.parent.name %></td>
isn't it working?
I'm fairly new to rails, and am still getting used to putting together methods. I'm currently trying to create a method that averages distinct data from multiple columns. I'd like to do it all in one method so that I can display the information easily in an html table.
Currently I have this in my model:
def averagedonate
scores.group(:donatedate).average('donateamount')
scores.group(:donatedate).average('rating')
end
I'd like to be able to use them in a table like this:
<% #averagedonate.each do |donatedate, donateamount, rating| %>
<tr>
<td><%= donatedate %></td>
<td><%= donateamount %></td>
<td><%= rating %></td>
</tr>
How do I change my averagedonate method to do this? Thanks in advance!
I haven't tested, but something to this effect should work
def averagedonate
scores.select("
AVG(donateamount) as avg_donateamount,
AVG(rating) as avg_rating,
donatedate
")
.group(:donatedate)
end
Then use it like this
<% #averagedonate.each do |item| %>
<tr>
<td><%= item.donatedate %></td>
<td><%= item.avg_donateamount %></td>
<td><%= item.avg_rating %></td>
</tr>
<% end %>
I'm a total newbie to rails, and want to stop making the same tedious reports in excel and create a marketing analytics dashboard.
I have a User model, with created_at, marketing_source, purchases, and revenue.
What I want to do in sql is
select marketing_source, weekofyear(created_at), count(id), sum(revenue)
from User
where weekofyear(created_at) between # and #
group by marketing_source, weekofyear(created_at)
and then print this as a table to my page.
I'm just not exactly sure where and how to do the transformation. Do I put this via
sql.execute
in the controller code? Should I create a rake task to make a csv and then use javascript to read the csv and print out the table?
Any direction would be extremely appreciated.
Welcome to rails
If you are new to rails check out railscasts.com and http://guides.rubyonrails.org/
In rails you don't have to write sql statements like this.
I am assuming you have done the database migrations and have all the necessary columns in you database
in your users_controller, I think this would do what you want.
def index
#users = User.where
(["created_at >= ? AND created_at <= ?", yesterday.beginning_of_day, yesterday.end_of_day]).
group("marketing_source")
end
Then in your corresponding view
/views/users/index.html.erb
<table>
<tr>
<th>Name</th>
<th>revenue</th>
<th>Marketing source</th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%=h user.name %></td>
<td><%=h user.revenue %></td>
<td><%=h user.marketing_source %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
I have two tables:
Chords - |id|chord|name|rating|artist_id|
Artists - |id|artist|
An Artist has many Chords, and thus a Chord belongs to an Artist.
And in the index page for "chords" I want to display chord, name, and rating from Chords table and the artist from the artists table
This is the code for the Chord's index.html.erb:
<table border="1">
<% #chords.each do |chord| %>
<tr>
<td><%= chord.artist.artist %></td>
<td><%= link_to chord.name, chord %></td>
<td><%= chord.rating %></td>
<td><%= chord.created_at %></td>
</tr>
<% end %>
</table>
The error message is:
undefined method `artist' for nil:NilClass
Actually, at first it worked, but when I started to create the "new.html.erb" page and the create and new actions, it stopped working, that's why this is so confusing to me!
Since chort.artist can be null, you should change chord.artist.artist to chord.artist.try(:artist) which is shorthand for
if chord.artist.nil?
nil
else
chord.artist.artist
end
You could use
<td><%= chord.artist.artist unless chord.artist.artist.nil? %></td>
In the view is no the right place to do it but what you need to do is find the artist with the ID given in the CHORDS like this
#currentArtist = Artist.find(:all, :conditions => {:protectora => Chord.artist_id})
Then when you find it by the ID given in the CHORD and save it in to a variable, you can access it like any other variable so:
#currentArtist.artist
Hope it helps.