ruby - Do not understand locals in rails - getting a No method error -


i new rails , trying shopping cart html view site admin when order made. email portion working fine simple html when add ruby code render views page following error:

nomethoderror in charges#create showing /home/ubuntu/workspace/app/views/order_mailer/order_email.html.erb line #7 raised:  undefined method `size' 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 have read think need add locals render code confused on locals , in code. if off track , error has nothing locals appreciate guidance in right direction.

thanks!

order_email view:

<h1>you have new order </h1> <%= render "carts/shopping_cart",  size: @order_items.size  %> 

the view trying render in email:

<% if !@order_item.nil? && @order_item.errors.any? %>       <div class="alert alert-danger">         <ul>         <% @order_item.errors.full_messages.each |msg| %>           <li><%= msg %></li>         <% end %>         </ul>       </div>     <% end %>     <% if @order_items.size == 0 %>       <p class="text-center">         there no items in shopping cart.  please <%= link_to "go back", root_path %> , add items cart.       </p>     <% else %>       <% @order_items.each |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 used when doing render partial, should able you'd need make few adjustments.

each of partials' filename need adjusted have , underscore in front of them, still reference them without underscore (i know it's bit confusing).

so rename carts/shopping_cart carts/_shopping_cart , change it's referenced to:

<%= render partial: "carts/shopping_cart", locals:{size: @order_items.size} %>

do same sort of thing 'carts/cart_row'


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -