"We are creating a ""displaying when your next purchase will be"" system in Rails."
·User table: Purchaser information registered
→ Column: id, number, name
·Product table: Product information registered
→ Column: id, number, brand, name, term
·Purchase table: Fill in the form
→Column: user_number, product_number
Create an Intermediate Table (Purchase) and
Registered user_number and product_number
Enter form and retrieve linked data from User and Product tables
The following error message occurred while implementing the feature:
NoMethodError in Purchases#index
Showing/home/ec2-user/environment/RepeatAlart_app/app/views/purchases/index.html.erb where line #18 raised:
undefined method `name' for 1: Integer
Extracted source (around line #18):
16<tr>
17<td><%=purchase.user_number%></td>
18<td><%=purchase.user_number.name%></td>
19<td><%=purchase.product_number.brand%>/td>
20<td><%=purchase.product_number.name%>/td>
21<td><%=purchase.created_at%>/td>
model
class User<ApplicationRecord
values:name,presence:true,length:{maximum:50}
values:number, presence:true
has_many —Purchases
has_many:products, through::purchases
belongs_to —salon
end
model
class Product <ApplicationRecord
values:brand,presence:true
values:number, presence:true
values:name, presence:true
values:term, presence:true
values:name, unique:true
has_many —Purchases
has_many:users,through::purchases
belongs_to —salon
end
model
class Purchase <ApplicationRecord
belongs_to:user, optional:true
belongs_to:product, optional:true
belongs_to —salon
end
controller
class PurchasesController <ApplicationController
def index
@purchases=Purchase.where(salon_id:current_salon.id)
@purchase=Purchase.new
end
def create
@purchase=Purchase.new(purchase_params.merge(salon_id:current_salon.id))
[email protected]
flash[:success] = "Success!"
redirect_to "/purchases"
else
redirect_to "/purchases"
end
end
default
@purchase=Purchase.find (params[:id])
end
default update
purchase=Purchase.find (params[:id])
purchase.update(purchase_params)
redirect_to "/purchases"
end
def destroy
Purchase.find(params[:id]).destroy
flash[:success] = "deleted"
redirect_to "/purchases"
end
private
def purchase_params
param.require(:purchase).permit(:user_number,:product_number)
end
end
view
<h1>List of purchased items</h1>
<table class="table">
<thead>
<tr>
<th>Customer No.</th>
<th>Customer Name</th>
<th>Brand </th>
<th>Product Name</th>
<th>Date of purchase</th>
<th>Next Purchase Recommendation Date</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<%@purchases.each do | purchase | %>
<tr>
<td><%=purchase.user_number%>/td>
<td><%=purchase.user_number.name%>/td>
<td><%=purchase.product_number.brand%>/td>
<td><%=purchase.product_number.name%>/td>
<td><%=purchase.created_at%>/td>
<td><%=purchase.limit_date%>/td>
<td><%=link_to "Edit", "#", class: "btn btn-primary mr-3" %>/td>
<td><%=link_to "delete", purchase, method::delete, class:"btn btn-primary mr-3"%>/td>
<%end%>
<tr>
</tbody>
</table>
<h1>Register Purchase Information</h1>
<div class="row">
<div class="col-md-6col-md-offset-3">
<%=form_for(@purchase)do|f|%>
<%=f.label: user_number%>
<%=f.number_field:user_number,class:'form-control'%>
<%=f.label:product_number%>
<%=f.number_field:product_number, class: 'form-control'%>
<%=f.submit "Register Purchase Information", class: "btn btn-primary" %>
<%end%>
</div>
</div>
in index.html.erb
purchase.user_number.name
purchase.user.name
and so on.
363 Logging Out with the Application Load Balancer and Authentication Using Cognito
354 Understanding How to Configure Google API Key
372 To find Python openpyxl value coordinates
364 winget install-e --id Microsoft.WindowsSDK fails.
354 JSON.parse fails even though there is no problem with the format.
© 2023 OneMinuteCode. All rights reserved.