Getting error of
Couldn't find ActiveStorage::Blob with 'id'=41
I am trying to read blog attachments and see the images and download them. It works sometimes and doesn't works when server is restarted.
My show.html.haml view template is
%h1.page-heading
%a{href: admin_tenant_site_unit_floor_plans_path}
%i.material-icons.left arrow_back
Listing Images
%small= "of Floor Plan #{#floor_plan.id}"
.row
.col.s8
%table.report
%thead
%tr
%th= "Blob_id"
%th= "Name"
%th= "Record type"
%th
%i.material-icons visibility
%th
%i.material-icons download
%tbody
- if #floor_plan.floor_image.attached?
- #floor_plan.floor_image.each do |image|
%tr
%td= image.blob_id
%td= image.name
%td= image.record_type
-# %td= link_to "Download " + image.record_type + " " + image.blob_id.to_s, rails_blob_path(image, disposition: "attachment"), target: '_blank'
%td= link_to "View " + image.record_type + " " + image.blob_id.to_s, url_for(image), target: '_blank'
%td= link_to "Download " + image.record_type + " " + image.blob_id.to_s, rails_blob_path(image, disposition: "attachment"), target: '_blank'
Controller action :
def show
# binding.b
#floor_plan = FloorPlan.find(params[:id])
end
Also index.html.haml :
= render(partial: 'admin/units/sublinks')
%h5.section-heading.mt20
Listing Floor plans
.row
.col.s8
%table.report
%thead
%tr
%th= "Name"
%th= sortable "created_at", "Submission Date"
%tbody
- #floor_plans.each do |plan|
%tr
%td= plan.name
%td= plan.created_at.strftime("%b %d, %Y %r")
%td
%a.dropdown-trigger.btn.waves-effect.waves-teal.btn-flat.right{"data-target" => "client_dropdown#{plan.id}", :href => "#"}
%i.material-icons.right>expand_more
More
%ul.dropdown-content{id: "client_dropdown#{plan.id}"}
%li
= link_to "Show", admin_tenant_site_unit_floor_plan_path(#tenant, #site, #unit, plan)
- if plan.floor_image.attached?
- plan.floor_image.each do |image|
- p "+++++++++++++++++++++++++++"
- p plan
- p image
- p image.blob_id
%li
= link_to "View " + image.record_type + " " + image.blob_id.to_s, url_for(image), target: '_blank'
%li
= link_to "Download " + image.record_type + " " + image.blob_id.to_s, rails_blob_path(image, disposition: "attachment"), target: '_blank'
%li
= link_to 'Edit name',
edit_admin_tenant_site_unit_floor_plan_path(#tenant, #site, #unit, plan)
%li
= link_to 'Delete', [:admin, #tenant, #site, #unit, plan], method: :delete, data: { confirm: 'Are you sure?' }
FloorPlan.rd model file :
class FloorPlan < ApplicationRecord
belongs_to :unit
has_many_attached :floor_image, dependent: :destroy
validates :name, presence:true
validates :floor_image, presence:true
end
And here is the snippet from schema.rb
create_table "floor_plans", force: :cascade do |t|
t.string "name", null: false
t.bigint "unit_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["unit_id"], name: "index_floor_plans_on_unit_id"
end
The error doesn't come up when the exactly same system I used in other component of the app. But the error is persistent upon clicking the "View" and "Download" links in show and index templates.
Please guide me to resolve this error and to understand the root cause.
Also I wanna print the name of the blob items instead of printing names from the active_storage_attachments table.
Related
I am trying to add providers to patients list. For example patients can have many providers. It's not working. My code may be wrong but any help will be appreciated.
<% #providers.each do |provider| %>
<div class="item" data-email="<%=provider.role.user.email%>" data-name="<%=provider.role.user.first_name%> <%=provider.role.user.last_name%>">
<div class="d-inline-block">
<p class="name"><%= provider.role.user.first_name %><%= " " + provider.role.user.middle_name if provider.role.user.middle_name %><%= " " + provider.role.user.last_name %></p>
<p class="name"><span>Professional Title: </span><%= provider.professional_title if provider.professional_title %></p>
</div>
<div class="d-inline-block float-right">
<%= link_to patient_add_provider_path(#patient, provider_join: #patient.id, provider_id: provider.id), method: :post, :data => {:confirm => 'Are you sure you want to add this site?'} do %> ####THIS IS WHAT CURRENTLY IS NOT WORKING(link_to)
<button class="btn-submit">Add</button>
</div>
</div><!--item-->
<% end %>
Here is a the method i have inside my patient_controller.rb file
def add_provider
flash[:modal]
#patient = current_user.current_role.roleable
#provider = Provider.find(params[:provider_id])
#provider_join = Provider_join.where(
:provider_id => params[:provider_id]).first_or_create do |provider_join|
provider_join.provider = params[:provider_id]
end
#provider_join.soft_delete = false
if #provider_join.save
flash[:success] = "You have successfully added a physician to your list"
redirect_back(fallback_location: root_path)
else
end
end
and here is my schema file
create_table "provider_joins", id: false, force: :cascade do |t|
t.bigint "provider_id"
t.bigint "patient_id"
t.boolean "soft_delete", default: false
t.index ["patient_id"], name: "index_provider_joins_on_patient_id"
t.index ["provider_id"], name: "index_provider_joins_on_provider_id"
end
and finally but not least here is the error i am receiving in the console
ActionView::Template::Error (undefined method `id' for nil:NilClass):
6: </div>
7:
8: <div class="d-inline-block float-right">
9: <%= link_to patient_add_provider_path(#patient, provider_join: #patient.id, provider_id: provider.id), method: :post, :data => {:confirm => 'Are you sure you want to add this Physician?'} do %>
10: <button class="btn-submit">Add</button>
11: <% end %>
12:
I'm pretty new in Rails and I need your help.
I have Job model, Invoice model and Item model.
job.rb model have - has_one :invoice, dependent: :destroy accepts_nested_attributes_for :invoice
invoice.rb have - belongs_to :job, optional: true, :foreign_key => 'job_id' has_many :items, dependent: :destroy accepts_nested_attributes_for :items
item.rb have - belongs_to :invoice, optional: true, :foreign_key => 'invoice_id'
Everything is ok, it creating the job and invoice with items inside and on invoice show it generate the invoice but in invoices/index I want to list all invoices in a table but I can't get = #invoice.job.customer_name.capitalize it give me a error - NoMethodError in Invoices#index undefined method `customer_name' for nil:NilClass.
It is like job is nil. My question is why in invoices/show.html.erb I can access
= #invoice.job.customer_name.capitalize and in index it give me error ?
As you can see in invoices/show.html.haml I have = #invoice.job.customer_name.capitalize and it give me correct data:
.container.invoice
.row
.col-sm-6
- if current_user.logo.present?
= image_tag current_user.logo.url(:thumb)
- else
= image_tag 'blank.jpg'
.col-sm-3
.col-sm-3
%h3
= current_user.full_name.titleize
%p
= current_user.email
%p
= current_user.phone
%hr
.row
.col-sm-6
%h4
Customer :
= #invoice.job.customer_name.capitalize.html_safe
%p
%b
Phone:
= #invoice.job.customer_tel
%p
%b
Email:
= #invoice.job.customer_email
.col-sm-3
.col-sm-3
%p
%b
Invoice: #
= #invoice.id
%p
%b
Date:
= #invoice.created_at.strftime("%B %d, %Y")
%hr
.row
.col-sm-12
%table.table.table-striped.table-hover
%thead
%tr.sortable-table
%th.col-sm-1
Item Id
%th.col-sm-3
Item/Operation
%th.col-sm-3
Unit cost/Hr rate
%th.col-sm-3
Qty
%th.col-sm-2
%th.col-sm-3
Amount
%tbody
- #invoice.items.each do |item|
%tr.sortable-table
%th
= item.id
%th
= item.name
%th
= number_to_currency item.unit_cost
%th
= item.qty
%th
%th
- amount = number_to_currency(item.unit_cost * item.qty)
= amount
%hr
.row
.col-sm-6
.col-sm-3
.col-sm-3
%b
Subtotal
- sum = 0
- #invoice.items.each do |item|
- if item.qty?
- subtotal = (item.unit_cost * item.qty)
- sum += subtotal
= number_to_currency(sum)
%p
%b
Tax
= number_to_currency(sum * (#invoice.tax/100))
%p
%h4
%b
Total
= number_to_currency((sum * (#invoice.tax/100)) + sum)
%hr
= link_to 'Edit', edit_invoice_path(#invoice)
\|
= link_to 'Back', invoices_path
But in invoices/_invoice.html.erb I have the error :
%h1 Listing invoices
%table.table.table-striped.table-hover
%thead
%tr.sortable-table
%th
Id
%th
Job Id
%th
Item
%th
Unit cost
%th
Qty
%th
Amount
%tbody
= render #invoices
%br
= link_to 'New Invoice', new_invoice_path
<tr>
<td>
<%= invoice.id %>
</td>
<td>
<%= invoice.job_id %>
</td>
<td>
</td>
<td>
<%= invoice.unit_cost %>
</td>
<td>
<%= invoice.qty %>
</td>
<td>
<%= invoice.job.customer_name %>
</td>
<td>
<%= link_to 'Details', invoice, class: 'btn btn-primary btn-xs' %>
</td>
<td>
<%= link_to "", edit_invoice_path(invoice), id: "edit_#{invoice.id}", class: 'glyphicon glyphicon-pencil index-icons' %>
</td>
<td>
<%= link_to "", invoice_path(invoice), method: :delete, id: "delete_invoice_#{invoice.id}_from_index", class: 'glyphicon glyphicon-trash index-icons', data: {confirm: 'Are you sure ?'} %>
</td>
</tr>
Thank you, and if you need more code let me know.
Update:
[you see invoice Id 47 have job id 67][1]
[the job id 67 is exist][2]
[on the invoice show I have name of customer][3]
[1]: https://i.stack.imgur.com/ODAkb.png
[2]: https://i.stack.imgur.com/uGK21.png
[3]: https://i.stack.imgur.com/sRTWS.png
NoMethodError in Invoices#index undefined method `customer_name' for
nil:NilClass.
This means where you are calling customer_name:
#invoice.job.customer_name.capitalize.html_safe
You are calling it on nil, or in other words:
#invoice.job
returns nil. If the invoice does not have a job associated with it, any methods involving the invoice's job will fail (apart from just #invoice.job, which will return nil).
Your two options are to either make sure every invoice has a job associated with it before rendering, or to rewrite your view template as:
.container.invoice
.row
.col-sm-6
- if current_user.logo.present?
= image_tag current_user.logo.url(:thumb)
- else
= image_tag 'blank.jpg'
.col-sm-3
.col-sm-3
%h3
= current_user.full_name.titleize
%p
= current_user.email
%p
= current_user.phone
%hr
.row
.col-sm-6
- if #invoice.job
%h4
Customer :
= #invoice.job.customer_name.capitalize.html_safe
%p
%b
Phone:
= #invoice.job.customer_tel
%p
%b
Email:
= #invoice.job.customer_email
.col-sm-3
.col-sm-3
%p
%b
Invoice: #
= #invoice.id
%p
%b
This will only display the bottom section of the view if a job is associated with the #invoice
Actually I am trying to use enum attribute in my rails 5 blog app.The problem is that when I try to toggle the status from draft to published or vice versa, I get the error stating "NoMethodError in BlogsController#toggle_status"
blogs_controller.rb
class BlogsController < InheritedResources::Base
def toggle_status
if #blog.draft?
#blog.published!
elsif #blog.published?
#blog.draft!
end
redirect_to blogs_url, notice: "Blog status has been updated"
end
private
def blog_params
params.require(:blog).permit(:title, :body)
end
end
index.html.slim
h1 Listing blogs
table
thead
tr
th Title
th Body
th
th
th
th
tbody
- #blogs.each do |blog|
tr
td = blog.title
td = blog.body
td = link_to blog.status,toggle_status_blog_path(blog)
td = link_to 'Show', blog
td = link_to 'Edit', edit_blog_path(blog)
td = link_to 'Destroy', blog, data: { confirm: 'Are you sure?' }, method: :delete
br
= link_to 'New Blog', new_blog_path
blog.rb
class Blog < ApplicationRecord
enum status: { draft: 0, published: 1 }
end
routes.rb
Rails.application.routes.draw do
resources :blogs do
member do
get :toggle_status
end
end
end
schema.rb
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
end
I don't know where I may be going wrong, I tried at my best but unable to figure it out.
Any suggestions are most welcome.
Thank you in advance.
You'll need to set your #blog instance variable.
def toggle_status
#blog = Blog.find(params[:id])
...
I have the following code to create a nested model (invoice_line_items) within my invoice model:
= nested_form_for #invoice, mutipart: true, class: "form-horizontal" do |f|
...
%table.table.table-bordered{:id => "line-items"}
%thead
%tr
%th
%th Description
%th Quantity
%th Rate
%tbody
%tr
= f.fields_for :invoice_line_items do |line_item|
%td= line_item.link_to_remove "X"
%td= line_item.text_field :description
%td= line_item.text_field :quantity, :class => "input-mini"
%td= line_item.text_field :rate, :class => "input-mini"
= f.link_to_add "Add", :invoice_line_items, :data => { :target => "#line-items" }
I have two problems: 1) when I add a new row by clicking "Add"....it doesn't match the table formatting and doesn't insert into the table. I tried everything to get it to work and it just doesn't. I also tried adding the ":target" as mentioned by ryanb in his gem docs. 2) I would like to have 3 invoice_line_items initially ready for the user on the invoice page, but i'm not sure how to do that.
EDIT: I got something a little different now since I've been playing around with it. I don't think i'm doing it right still but now it creates a new form each time I click "add":
.row-fluid
= f.fields_for :invoice_line_items, :wrapper => false do |line_item|
%table.table.table-bordered#tasks
%thead
%th
%th Description
%th Quantity
%th Rate
%tr.fields
%td= line_item.link_to_remove "X"
%td= line_item.text_field :description
%td= line_item.text_field :quantity, :class => "input-mini"
%td= line_item.text_field :rate, :class => "input-mini"
.row-fluid
= f.link_to_add "Add", :invoice_line_items, :data => { :target => "#tasks" }
This was a tough one to figure out but it turns out that the most updated version of the nested_form gem doesn't come with the most recent version of the jquery_nested_form.js file (even though the documentation says to use this function). So, to get this to work, I had to add this to my application.js:
$(function ($) {
window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
var target = $(link).data('target');
if (target) {
return $(content).appendTo($(target));
} else {
return $(content).insertBefore(link);
}
};
});
this overwrote the function in jquery_nested_form.js and now everything works.
I am creating an equipment and I have two actions in my equipment controller, new and create.. and I have two views which is new and a partial form..
I also have four tables.. which are equipments, equipment_types, equipment_locations, and equipment_management_informations..
The form looks like..
>= error_messages_for 'equipment'
%table.contentText{:style => "width: 100%"}
- if #types
%tr
%td Equipment Type
%td= collection_select 'equipment', 'equipment_type_id', #types, :id, :name, {}, :class => "dropdownSelect"
%tr
%td Location
%td= select 'equipment', 'equipment_location_id', #equipment_locations.collect { |e| ["#{e.name.capitalize!}", e.id]},{}, :class => "dropdownSelect"
%tr
%td Serial Number
%td= text_field 'equipment', 'serial_number', :class => 'textFields'
%tr
%td MAC Address
%td= text_field 'equipment', 'mac_address', :class => 'textFields'
%tr
%td IP Address
%td= text_field 'equipment', 'ip_address', :class => 'textFields'
- if #stands
%tr
%td Stand
%td= collection_select 'equipment', 'stand_id', #stands, :id, :street_no, :include_blank => true
%tr
%td{:valign => 'top'} Description
%td= text_area 'equipment', 'description', :cols => 45, :rows => '3', :class => "txt_area_effect"
and my new view is like:
> %h3 New equipment
%div{:style => "border: 1px solid #CCC;"}
- form_tag :action => 'create', :estate_id => #estate do
= render :partial => 'form'
= submit_tag "Create"
- action = "list"
%input{:type => "button", :value => "Back", :onclick => "window.location='#{action}';", :class => "start"}
and my create action display this a nil when i inspect it
#equipment = Equipment.find(params[:equipment])
it gives an error like
Unknown key(s): equipment_type_id, mac_address, description, equipment_location_id, serial_number, ip_address
#equipment = Equipment.find(params[:equipment])
should be:
#equipment = Equipment.new(params[:equipment])
or
#equipment = Equipment.new(params[:equipment])
(just posting answer from comments so this stops showing as unanswered)