Hello I have a Rails application in production, and recently i started getting totally random errors on the loading of the show method of my "complaints" controller .
My "complaints" table has a one-to-one relationship with "interactions" table, you can create an interaction then create a complaint.
I display my data in a table on complaints#index, then I click on a row to get to complaints#show
I get this error :
ActionView::Template::Error (undefined method `[]' for nil:NilClass):
7: <div class="uk-width-expand#m">
8: <h1 class="uk-heading-primary">
9: <%# <%= link_to 'Retour à l\'interaction', interaction_path(#variables[:complaint].interaction, search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
10: <%= I18n.t("kind.#{#variables[:complaint].kind}") %>
11: <% if #variables[:complaint].interaction.date.present? %>
12: - <%= #variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
13: <% end %>
app/views/complaints/show.html.erb:10:in `_app_views_complaints_show_html_erb__763547916_154685640'
But this error appears totaly randomly. That means if I refresh the page, it may launch this time. So because of this, i can't understand where this error comes from, most of the time there is no error, but I get complaints from the app users saying they are really bothered.
Here is my controller show method :
def show
complaint = Complaint.find_by(id: params[:id])
if complaint.blank?
redirect_to complaints_path
return
end
interaction = Interaction.find(complaint.interaction.id)
result = DetailCommande.execute_procedure "p_detail_commande", interaction.do_piece, interaction.do_type
contenu_commande = DetailCommande.execute_procedure "p_contenu_commande", interaction.do_piece, interaction.do_type
#variables = { complaint: complaint, detail_commande: result[0], contenu_commande: contenu_commande }
#params = { search: params[:search] }
end
Here is the part of my complaints#show view that causes trouble :
<div class="uk-flex-middle uk-margin-small" uk-grid>
<div class="uk-width-expand#m">
<h1 class="uk-heading-primary">
<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
<%= I18n.t("kind.#{#variables[:complaint].kind}") %>
<% if #variables[:complaint].interaction.date.present? %>
- <%= #variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
<% end %>
</h1>
</div>
<div class="uk-width-auto#m">
<% if #variables[:complaint].control.present? %>
<button class="uk-button uk-button-default" disabled>Contrôle</button>
<% else %>
<a href="#modal-control-<%= params[:id] %>" class="uk-button uk-button-warning" uk-toggle>Contrôle</a>
<% end %>
<%= link_to 'Modifier', edit_complaint_path(id: #variables[:complaint].id, search: #params[:search]), class: 'uk-button uk-button-primary' %>
<%= link_to 'Supprimer', complaint_path(#variables[:complaint], search: #params[:search]), method: :delete, data: {confirm: 'Êtes-vous sûr ?'}, class: 'uk-button uk-button-danger' %>
</div>
</div>
And here is a screen of my page when it actually loads. Just simple data display
I don't know if i provided enough informations, but thank you in advance for your help
I think that your application is running on 2 servers , sharing the load by load balancer.
I think so because of the following 2 cases:
See line no. 9 of your log i.e.
9: <%# <%= link_to 'Retour à l\'interaction', interaction_path(#variables[:complaint].interaction, search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
and your html file does not contains this code, this are some changes in show.html i.e.
<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
This error gone on page refresh, it means that 1 server contains correct code and 1 contains wrong code, so when the request goes on server 1, it displays the page and when it goes on server2, it gives error.
As per the logs, I think that complaint.kind is empty.
Related
i am on rails 7 trying out the turbo frame.
supposedly my code should fire a Response has no matching error because i do not have the similar id in the new page after clicking the new link. however it just loads the new page as usual, contradict with a tutorial. any idea how do i get the error logged instead of loading a new page? thanks.
# app/views/quotes/index.html.erb
<main class="container">
<%= turbo_frame_tag "first_turbo_frame" do %>
<div class="header">
<h1>Quotes</h1>
<%= link_to "New quote", new_quote_path, class: "btn btn--primary" %>
</div>
<% end %>
<%= render #quotes %>
</main>
# app/views/quotes/new.html.erb
<h1>New quote</h1>
<%= render "form", quote: #quote %>
Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>
I have a partial that I am using on several pages that require an array to be passed from the views.
<div id="help-modal" class="hidden">
<%= render 'unit_setup_help', troubleshoot: "Unit Troubleshooting", message: <%= yield(:message_array) %> %>
</div>
I want message: message_array, with message_array be passed via the view.
I tried doing message: "#{yield(:message_array)}" but then I get an array with nested inside quotes. For example:
<% msg_array = ["Don't choose wrong one or you will die" ,"choose wrong one or you will die"] %>
<% content_for :message_array do %>
<%= msg_array %>
<% end %>
I get this error:
undefined method `each' for #
And this what my result looks like:
=> " ["Don't choose wrong one or you will die", "choose wrong one or you will die"]\n"
I am fairly new to rails and I am trying to the shopping cart html view to the site admin when a order is made. The email portion is working just fine with simple html but when I add the ruby code to render the views page I get the following error:
NoMethodError in Charges#create
Showing /home/ubuntu/workspace/app/views/order_mailer/order_email.html.erb where line #7 raised:
undefined method `size' for nil:NilClass
Extracted source (around line #7):
5
6
7
<%= render "carts/shopping_cart", size: #order_items.size %>
Rails.root: /home/ubuntu/workspace
Application Trace | Framework Trace | Full Trace
app/views/order_mailer/order_email.html.erb:7:in `_app_views_order_mailer_order_email_html_erb___1590505826361550286_70123907983000'
app/mailers/order_mailer.rb:11:in `order_email'
app/controllers/charges_controller.rb:10:in `create'
From what I have read I think I need to add locals to my render code but I am confused on what exactly locals are and what they would be in my code. If I am completely off track and this error has nothing to do with locals I would appreciate some guidance in the right direction.
Thanks!
order_email view:
<h1>You have a new order </h1>
<%= render "carts/shopping_cart", size: #order_items.size %>
The view I am trying to render in the email:
<% if !#order_item.nil? && #order_item.errors.any? %>
<div class="alert alert-danger">
<ul>
<% #order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if #order_items.size == 0 %>
<p class="text-center">
There are no items in your shopping cart. Please <%= link_to "go back", root_path %> and add some items to your cart.
</p>
<% else %>
<% #order_items.each do |order_item| %>
<%= render 'carts/cart_row', product: order_item.product, order_item: order_item, show_total: true %>
<% end %>
order_mailer.rb:
class OrderMailer < ApplicationMailer
default from: "xxxx#gmail.com"
def order_email(order_items)
#order_items = order_items
mail(to: "xxxx#gmail.com", subject: "Your subject")
end
end
I believe locals are only used when doing a render partial, which you should be able to do for this but you'd need to make a few adjustments.
Each of the partials' filename need to be adjusted to have and underscore in front of them, but you still reference them without the underscore (I know it's a bit confusing).
So rename carts/shopping_cart to carts/_shopping_cart
and then change where it's referenced to:
<%= render partial: "carts/shopping_cart", locals:{size: #order_items.size} %>
Do the same sort of thing with 'carts/cart_row'
I am having a weird issue where my rails application seemingly isn't reading the local variable passed to the partial, but only inside of form_for.
I have a set up a button where a user can bookmark an object, let's call it zoo.
The code looks like this:
Zoo Listing View:
<li><%= render 'directory/bookmarks', zoo: zoo %></i></li>
Then the directory/bookmarks partial:
<% if user_signed_in? %>
<% if current_user.has_zoo_bookmarked_already? %>
<%= form_for(current_user.bookmarks.find_by_zoo_id(zoo.id), html: { method: :delete }, remote: true) do |f| %>
<%= button_tag do %>
<button class="btn btn-lg btn-block btn-primary">remove bookmark
<% end %>
<% end %>
<% else %>
<%= form_for(current_user.bookmarks.build(zoo_id: zoo.id)) do |f| %>
<%= f.hidden_field :zoo_id, value: zoo.id %>
<%= button_tag do %>
<button class="btn btn-lg btn-block btn-primary">bookmark
<% end %>
<% end %>
<% end %>
<% else %>
<% end %>
Anyway, the problem is the zoo.id on the 3rd line is not being evaluated. I get this error.
undefined method `model_name' for NilClass:Class
The weird part is that locals are being read on the page.
If I place this code snippet <%= zoo.id %> anywhere on the page, I get the id. Then, the find_by_zoo_id is being evaluated also...if I put 255 a hardcoded number, it works. Or if I set it to a variable, #id = 255 and then pass in the #id, it works.
The zoo.id only does not work inside of the form_for for some reason. If I set it to an instance variable on the page, #id = zoo.id, that doesn't work either.
Any ideas? I'm sure it is something minor.
EDITED:
The relationship is has_many :through. Forgot to mention, I use this code for a different association in the same application and it works fine.
That error you're getting suggests current_user.bookmarks.find_by_zoo_id(zoo.id) is nil. find_by_zoo_id might be returning you nothing
Of course it was -- I was iterating through a list of zoos with bookmarks, but not all of them would be bookmarked by the user. I just had to add an additional condition.
<% if user_signed_in? %>
<% if current_user.has_zoo_bookmarked_already? && current_user.bookmarks.find_by_zoo_id(zoo.id) %>
#....