Rails active storage - Download link - ruby-on-rails

I am trying to create a download link with active storage to download whatever files has been uploaded using the
<%= link_to 'download', rails_blob_path(f, disposition: "attachment") %>
But instead it's showing me undefined method filename for #<Order id: 1, paper_size: A4....
How can i fix this??
index.html.erb:
<div class="h1">Admin Dashboard</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Size</th>
<th scope="col">Color</th>
<th scope="col">Type</th>
<th scope="col">Quantity</th>
<th scope="col">Description</th>
<th scope="col">Downloads</th>
</tr>
</thead>
<tbody>
<% #orders.each do |f| %>
<tr>
<th scope="row"><%= f.id %></th>
<td><%= f.first_name %></td>
<td><%= f.last_name %></td>
<td><%= f.phone_number %></td>
<td><%= f.email %></td>
<td><%= f.paper_size %></td>
<td><%= f.color %></td>
<td><%= f.paper_style %></td>
<td><%= f.quantity %></td>
<td><%= f.description %></td>
<% if f.files.attached? %>
<td><%= link_to 'download', rails_blob_path(f, disposition: "attachment") %></td>
<% end %>
<% end %>
</tr>
</tbody>
</table>
According to the tutorials and documentation, it said we need to use the rails_blob_path function to create downloads but when I use it I am getting an error saying 'undefined method filename'.
I am trying to create a download link inside of a table when i use:
<% if f.files.attached? %>
<td><%= link_to 'download', root_url %></td>
<% end %>
It works and redirects me to root path which indicates that f.files.attached? is returning TRUE, but when i call the rails_blob_path function its not working.

You need to pass Active Storage instances to the rails_blob_path path (instead of the Active Record parent object). Since it seems you have multiple files, it should be something like (iterate through all "files" and create a link for each one):
<% f.files.each do |file| %>
<%= link_to 'download', rails_blob_path(file, disposition: "attachment") %>
<% end %>

Related

Sort Date (Newest to Oldest) ROR

this is index.html.erb
<%= link_to_modal_new(new_master_film_path, "Enter film") %>
<%= link_to_export("Master films", params) %>
<%= paginate #master_films %>
<table class="table table-condensed table-hover">
<thead>
<tr>
<th></th>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= render #master_films %>
</tbody>
</table>
I need to organize the Date column in desc (newest to oldest) dates. I added the following dropdown option but it fails. Error template. Or if I can default the sort to newest Laminated date is first.
<%= render 'shared/sort_dropdown', current: sort, choices: [['serial','desc'], ['laminated','desc']] %>
I am writing this in detail, assuming you are very new to rails because of the way you asked the question and the way your code of view is. My apologies, if I am wrong.
In your controller (I guess it should be films_controller.rb or master_films_controller.rb), the action in which you are calculating this #master_films (it will have same name as the view that has the code you posted in question), add this:
#master_films = MasterFilm.all.order('laminated DESC')
# OR
#master_films = MasterFilm.all.order(laminated: 'desc')
# letter case does not matter
Your table code in view is not correct. It should be:
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= #master_films.each do |master_film| %>
<tr>
<td><%= master_film.serial %></td>
<td><%= master_film.formula %></td>
<td><%= master_film.mix %></td>
<td><%= master_film.mach %></td>
<td><%= master_film.ito_top %></td>
<td><%= master_film.thinky %></td>
<td><%= master_film.chemist %></td>
<td><%= master_film.operator %></td>
<td><%= master_film.inspector %></td>
<td><%= master_film.eff_w %></td>
<td><%= master_film.eff_l %></td>
<td><%= master_film.yield %></td>
<td><%= master_film.defects %></td>
<td><%= master_film.laminated %></td>
<td><%= master_film.note %></td>
<tr>
</tbody>
</table>

I'm trying to complete a table iterating in groups of data

I know this code is wrong, but I want to know which is the correct form to do it, I'm trying to iterate the groups, and each group have 8 teams
<table class="table">
<thead>
<tr>
<% #groups.each do |group| %>
<th><%= group.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #groups.each_slice(4) do |group_a, group_b, group_c, group_d| %>
<% group_a.teams.each do |team_a|, group_b.teams.each do |team_b|, group_c.teams.each do |team_c|, group_d.teams.each do |team_d| %>
<tr>
<td><%= team_a.name %></td>
<td><%= team_b.name %></td>
<td><%= team_c.name %></td>
<td><%= team_d.name %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Solution actually depends on the table structure you want to see, which you haven't clarified in the question. Here is an attempt; see how this ends up:
<table class="table">
<tbody>
<thead>
<th>Team#1</th>
<th>Team#2</th>
<th>Team#3</th>
<th>Team#4</th>
</thead>
<% #groups.each do |group| %>
<tr>
<th colspan='4'><%= group.name %></th>
</tr>
<tr>
<% group.teams.each do |team| %>
<td><%= team.name %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Thanks #JagdeepSingh for your help, after try and try this is what im looking for bro:
`<table class="table">
<thead class="thead-dark">
<tr>
<% #groups.each do |group| %>
<th><%= group.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #groups.each_slice(4) do |group_a, group_b, group_c, group_d| %>
<% for x in 0..7 do %>
<tr>
<td><%= group_a.teams[x].name %></td>
<td><%= group_b.teams[x].name %></td>
<td><%= group_c.teams[x].name %></td>
<td><%= group_d.teams[x].name %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>`

How to make a nested table in rails

I want to create a table named groups that lists of each group. Inside of each group, I want to list the groups courses. The group has_many :courses, and the course belongs_to :group. My current attempt does not work, and I have no idea where to go from there
Current Attempt
courses_controller.rb
def index
#courses = Course.find_by(group: :group_id)
#groups = Group.all
end
index.html.erb
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
<td><%= link_to 'Show', group %></td>
<td><%= link_to 'Edit', edit_group_path(group) %></td>
<td><%= link_to 'Destroy', group, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>
<% if current_user.master? %>
<td><%= link_to 'Edit', edit_course_path(course) %></td>
<td><%= link_to 'Destroy', course, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.master? %>
<%= link_to 'New Course', new_course_path %>
<% end %></td>
</tr>
<% end %>
</tbody>
</table>
Any help is appreciated :)
UPDATE
:group_id is the groups id for each created course, since the group has_many :courses The lesson belongs_to :course but that isnt important, as I just want the table, Thanks XP
As I said in my comment, what do you mean it isn't working? Are you getting any error messages? How have you structured you models? Have you created a migration to add group id to courses? What does your routes file look like - are courses nested under groups? If you don't provide this kind of information, its difficult to provide help.
If you look in the logs, you'll get some messages which will direct you to where things are going wrong. Have you tried running
Course.find_by(group: :group_id)
in a rails console. I suspect you'll find that this line is causing problems. Comment out this line and amend your erb as follows (I've not included all the code, but just to give you an idea). Also its not a good idea to use tables within tables.
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #group.courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>

search the contents of a table in rails

I want to place a searchbox on my table to filter it according to eventtitle.
Any ideas how I can go about this? I've tried two tutorials but it didn't work. I've also tried the filterrific gem but it didnt work either.
So far I just have this loading my table in the controller
#newevents = Event.order("id").page(params[:page]).per(50)
And this in my view
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>EventName</th>
<th>Date</th>
<th>Time</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<% #newevents.each do |ne| %>
<% if ne.eventready.blank?%>
<tr>
<td><%= ne.id %></td>
<td><%= ne.eventname %></td>
<td><%= ne.date %></td>
<td><%= ne.time %></td>
<td><%= link_to "Edit Event", edit_event_path(ne.id), class: "btn btn-info" %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= paginate #newevents %>

will_paginate (getting undefined method error for boards)

Maybe this is a newbie issue but I just can't figure out was going on
Getting an error when I refresh local host
undefined local variable or method `boards' for #<#:0x3c8eff8>
Extracted source (around line #14):
Here is the code in my index.html.erb for boards
Listing boards
<table>
<thead>
<tr>
<th>Name</th>
<th>About</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<%= will_paginate(boards) %>
<% #boards.each do |board| %>
<tr>
<td><%= board.name %></td>
<td><%= board.about %></td>
<td><%= board.user.full_name %></td>
<td><%= link_to 'Show', board %></td>
<td><%= link_to 'Edit', edit_board_path(board) %></td>
<td><%= link_to 'Destroy', board, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Board', new_board_path %>
****Here is the code for my index method in board controller****
def index
if user_signed_in?
#boards = current_user.boards
else
#boards = Board.paginate(page: params[:page], per_page: 1)
end
end
I have added the will_paginate to my gem file
source 'https://rubygems.org'
gem 'will_paginate'

Resources