on my e-store website when I try to checkout my cart, I'm getting
undefined method `items' for nil:NilClass.
Although on the error page
I know that my cart is there... but when I call it, it gives me nil
Cart Model
class Cart
attr_reader :items
def self.build_from_hash hash
items = if hash["cart"] then
hash["cart"]["items"].map do |item_data|
CartItem.new item_data["product_id"], item_data["quantity"]
end
else
[]
end
new items
end
def initialize items = []
#items = items
end
def add_item product_id
item = #items.find { |item| item.product_id == product_id }
if item
item.increment
else
#items << CartItem.new(product_id)
end
end
def empty?
#items.empty?
end
def count
#items.length
end
def serialize
items = #items.map do |item|
{
"product_id" => item.product_id,
"quantity" => item.quantity
}
end
{
"items" => items
}
end
def total_price
#items.inject(0) { |sum, item| sum + item.total_price }
end
end
Application Controller
def initialize_cart
#cart = Cart.build_from_hash session
end
Cart Controller
class CartsController < ApplicationController
before_filter :initialize_cart
def add
#cart.add_item params[:id]
session["cart"] = #cart.serialize
product = Product.find params[:id]
redirect_to :back, notice: "Added #{product.name} to cart."
end
def show
end
def checkout
#order_form = OrderForm.new user: User.new
end
end
Order Controller
class OrdersController
def create
#order_form = OrderForm.new(
user: User.new(order_params[:user]),
cart: #cart
)
if #order_form.save
redirect_to '/', notice: "Thank you for placing your order."
#cart.empty?
else
render 'carts/checkout'
end
end
Checkout View
<div class="container-checkout">
<p class="text-title"> You are checking out the following: </p>
<table class="table table-striped">
<thead class="name-table">
<tr>
<td> Image </td>
<td> Name </td>
<td> Category</td>
<td> Size </td>
<td> Item Price </td>
</tr>
</thead>
<tbody>
<% #cart.items.each do |item| %>
<tr>
<td><img src="<%= item.product.image %>" width="50px"></td>
<td><%= item.product.name.capitalize %></td>
<td><%= item.product.category.name %></td>
<td><%= item.product.size %></td>
<td class="price-item"><%= number_to_currency item.total_price %>
</td>
<% end %>
</tr>
<tr class="total-price total-price-checkout">
<td class="name-table">Total Price</td>
<td class="price-item"><%= number_to_currency #cart.total_price %></td>
</tr>
</tbody>
</table>
</div>
<div class="details-user-form">
<%= form_for #order_form, url: orders_path do |f|%>
<% f.fields_for :user, #order_form.user do |u| %>
<p class="text-title">Fill the form with your details</p>
<p><%= render "orders/errors" %></p>
<p><%= u.text_field :name, placeholder: "Name" %></p>
<p><%= u.text_field :email, placeholder: "Email" %></p>
<p><%= u.text_field :address, placeholder: "Address" %></p>
<p><%= u.text_field :postal_code, placeholder: "Postal code" %></p>
<p><%= u.text_field :city, placeholder: "City" %></p>
<p><%= u.text_field :country, placeholder: "Country" %></p>
<%= f.submit "Place order", class: "order-btn"%><br>
<% end %>
<% end %>
</div>
Any idea of why is it doing so? Also because, it was working before.. I don't know why it stopped.
I think the problem may be that the #cart variable isn't being set in the OrdersController. Setting the variable in CartsController doesn't make it available globally, as it would only be scoped to the controller that created it, in your case the CartsController.
Also, I see that your Cart model is more of a virtual model than an ActiveRecord model, is that the behaviour you were looking for as I believe ActiveRecord already has a lot of the methods you're recreating there.
I'm not totally sure but I think these may be the issues.
UPDATE
I think I found your error.
In your OrdersController you should have a
before_action :initialize_cart
That seems to be coming from your ApplicationController
If you check your checkout method in your CartController, you will see that you did not set #cart. So when you hit the checkout view, it comes to look for the value or #cart in this method. Setting it there, like the code below should clear your error.
def checkout
#order_form = OrderForm.new user: User.new
#cart = # cart object
end
Related
my controller
class IndustryController < ApplicationController
def index
#industries=Industry.all
end
def new
#industry=Industry.new
end
def create
#industry = Industry.new(industry_params)
# #industry = Industry.new(params[:name_bef,:name,:status])
# #industry = Industry.new(:name_bef => (params[:name]),:name => (params[:name]), :status => (params[:status]))
if #industry.save
redirect_to industry_index_path
else
render 'new'
end
end
def update
#industry = Industry.all
#industry.each do |i|
#industry.find(i.id)
#industry.update(industry_params)
end
end
private
def industry_params
params.require(:industry).permit(:name, :status)
end
private
def industry_update
params.require(:industry).permit(:name, :status)
end
end
my html
<%= form_for #industries, as: :industry, url: industry_path(#industries), method: :patch do |f| %>
<table class="table table-bordered">
<thead class="bg-light">
<tr>
<th scope="col">業種名(変更前)</th>
<th scope="col">業種名</th>
<th scope="col">状態</th>
</tr>
</thead>
<tbody>
<% #industries.each do |industry| %>
<tr>
<td><%= industry.name %></td>
<td>
<div class="form-group">
<%= f.text_field :name, :value => industry.name, :id => industry.id, :class => "form-control" %>
</div>
</td>
<td>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="form-group text-center">
<%= f.submit"登録", :class => " btn btn-success" %>
</div>
<%end%>
My assumption is that you are talking about the update action in your controller. It looks like update_all is the method you are looking for.
def update
Industry.update_all(industry_params)
end
This can be dangerous - be aware that this will bypass active record validations and callbacks, so make sure you are certain.
I am trying to set "edit" function for a simple CMS. I can make it to create/delete, but it just won't let me "edit".
here is the error message:
SyntaxError in SectionsController#edit
app/views/sections/edit.html.erb:42: syntax error, unexpected keyword_ensure, expecting keyword_end
Extracted source (around line #42):
40
41
when I checked my edit.html.erb. it seems fine?
'index'}, :class => 'back-link') %>
<div class="sections edit">
<h2>Update Sections</h2>
<%= form_for(:two, :url => {:action => 'update', :id => #one.id}) do |f| %>
<table summary="Section form fields">
<% #one.each do |f| %>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
<tr>
<th>content_type</th>
<td><%= f.text_field(:content_type) %></td>
</tr>
<tr>
<th>content</th>
<td><%= f.text_field(:content) %></td>
</tr>
<tr>
<th>page_id</th>
<td><%= f.text_field(:page_id) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Update Section") %>
</div>
<% end %>
</div>
Here is the controller:
class SectionsController < ApplicationController
def index
#one = Section.all
end
def show
#one = Section.find(params[:id])
end
def new
#one = Section.new
end
def create
#one = Section.new(section_params)
if #one.save
flash[:notice] = "Section created successfully!"
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#one = Section.find(params[:id])
end
def update
#one = Section.find(params[:id])
if #one.update_attributes(section_params)
flash[:notice] = "Subject updated successfully!"
redirect_to(:action => 'show', :id =>#one.id)
else
render('edit')
end
end
def delete
#one = Section.find(params[:id])
end
def destroy
subject = Section.find(params[:id]).destroy
flash[:notice] = "Subject deleted successfully!"
redirect_to(:action => 'index')
end
private
def section_params
params.require(:two).permit(:id,:name,:position,:visible,:page_id,:content,:content_type)
end
end
Thanks so much!!
Thanks #Pavan, #MarsAtomic and #Mandeep ! I found the issue: <% #one.each do |f| %>. I don't really need this line. If I need it, I need a end code for this.
In my case, I don't, so I go ahead deleted this line, and it worked now!
thanks again everyone!
So there isn't an error, my join table just isn't being populated with any information... #lead.districts returns District::ActiveRecord_Associations_CollectionProxy rather than the expected text...
Where have I made my mistake?
new.html.erb
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects new">
<h2>Create Lead</h2>
<%= form_for(:lead, :url => {:action => 'create'}) do |f| %>
<table cellspacing="0">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name)%></td>
</tr>
<tr>
<th>Phone Number</th>
<td><%= f.text_field(:phone_number)%></td>
</tr>
<tr>
<th>Email</th>
<td><%= f.text_field(:email)%></td>
</tr>
<tr>
<th>Move-in Date</th>
<td><%= f.date_field(:move_in_date, min: Date.today)%></td>
</tr>
<tr>
<th>Bedrooms</th>
<td><%= f.text_field(:beds)%></td>
</tr>
<tr>
<th>Bathrooms</th>
<td><%= f.text_field(:baths)%></td>
</tr>
<tr>
<th>Maximum Price Considered</th>
<td><%= f.text_field(:maxprice)%></td>
</tr>
<tr>
<th>Preferred Neighborhoods</th>
<td>
<%= collection_check_boxes(:lead, :district_ids, District.all.order("districts.name ASC"), :id, :name )%>
</td>
</tr>
<tr>
<th>Have you ever broken a lease?</th>
<td>
<%= f.label :broken_lease, "Yes", :value => true %>
<%= f.radio_button :broken_lease, true %>
<%= f.label :broken_lease, "No", :value => false %>
<%= f.radio_button :broken_lease, false %>
</td>
</tr>
<tr>
<th>Have you ever been convicted of a felony?</th>
<td>
<%= f.label :felon, "Yes", :value => true %>
<%= f.radio_button :felon, true %>
<%= f.label :felon, "No", :value => false %>
<%= f.radio_button :felon, false %>
</td>
</tr>
<tr>
<th>Do you have any pets?</th>
<td>
<%= f.label :pets, "Yes", :value => true %>
<%= f.radio_button :pets, true %>
<%= f.label :pets, "No", :value => false %>
<%= f.radio_button :pets, false %>
</td>
</tr>
<tr>
<th>If so, please provide a brief description</th>
<td><%= f.text_area(:pets_description)%></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Lead") %>
</div>
<% end %>
</div>
leads_controller.rb
class LeadsController < ApplicationController
layout false
def index
#leads = Lead.order("leads.created_at DESC")
end
def show
#lead = Lead.find(params[:id])
end
def new
#lead = Lead.new
#districts = District.all.order("districts.name ASC")
end
def create
#lead = Lead.new(lead_params)
if #lead.save
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#lead = Lead.find(params[:id])
end
def update
#lead = Lead.find(params[:id])
if #lead.update_attributes(lead_params)
redirect_to(:action => 'show', :id => #lead.id)
else
render('edit')
end
end
def delete
#lead = Lead.find(params[:id])
end
def destroy
lead = Lead.find(params[:id]).destroy
flash[:notice] = "Lead '#{lead.id}' deleted successfully."
redirect_to(:action => 'index')
end
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district => [])
end
end
districts_controller.rb
class DistrictsController < ApplicationController
layout false
def index
#districts = District.order("districts.name ASC")
end
def show
#district = District.find(params[:id])
end
def new
#district = District.new
end
def create
#district = District.new(district_params)
if #district.save
flash[:notice] = "District created successfully."
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#district = District.find(params[:id])
end
def update
#district = District.find(params[:id])
if #district.update_attributes(district_params)
flash[:notice] = "District updated successfully."
redirect_to(:action => 'show', :id => #district.id)
else
render('edit')
end
end
def delete
#district = District.find(params[:id])
end
def destroy
district = District.find(params[:id]).destroy
flash[:notice] = "District '#{district.name}' deleted successfully."
redirect_to(:action => 'index')
end
private
def district_params
params.require(:district).permit(:name)
end
end
district.rb
class District < ActiveRecord::Base
has_and_belongs_to_many :leads
end
lead.rb
class Lead < ActiveRecord::Base
has_and_belongs_to_many :districts
end
Join table migration:
class CreateDistrictsLeadsJoin < ActiveRecord::Migration
def change
create_table :districts_leads, :id => false do |t|
t.integer "district_id"
t.integer "lead_id"
end
add_index :districts_leads, ["district_id", "lead_id"]
end
end
My error was in mass assigning my params (used :district should have been :district_ids)
Correct params function:
# leads_controller.rb
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district_ids => [])
end
You will need to add leads to district and districts to leads in both create methods.
def create # in LeadsController
#lead = Lead.new(lead_params)
if #lead.save
district = District.find(...) #find district to associate
district.leads << #lead #add lead to district
redirect_to(:action => 'index')
else
render('new')
end
end
#leads_controller.rb
def create
#lead = Lead.new(lead_params)
if #lead.save
selected_dis = params['lead_district_ids'] #[3,4,5]
districts = District.where(id: selected_dis).first
#check how you getting district params and use that to fetch orders.
districts.each do |d|
#lead.districts << d
#assumming your one lead can have many districts.
end
redirect_to leads_path
else
render :new
end
end
I'm working through the rails intro guide but using 'stocks' instead of 'articles' and 'time_detlas' instead of 'comments' my issue is that it seems to be saving time_deltas correctly, I think I checked that correctly but the show of the stock just adds an extra blank row to the table of time_deltas no numbers show. Any suggestions why?
Stocks controller:
class StocksController < ApplicationController
def new
#stock = Stock.new
end
def index
#stocks = Stock.all
end
def create
# XXX Add columns for delta and current standing when we get there
# they can intiate to nil
#stock = Stock.new(stock_params)
if #stock.save
redirect_to #stock
else
render 'new'
end
end
def show
#stock = find_stock
#time_delta = #stock.time_deltas.build
end
def edit
#stock = find_stock
end
def update
#stock = find_stock
if #stock.update(stock_params)
redirect_to #stock
else
render 'edit'
end
end
def destroy
#stock = find_stock
#stock.destroy
redirect_to stocks_path
end
private
def stock_params
params.require(:stock).permit(:name, :hashtag)
end
def find_stock
return Stock.find(params[:id])
end
end
Time Delta Controller
class TimeDeltasController < ApplicationController
def create
#stock = Stock.find(params[:stock_id])
#time_delta = #stock.time_deltas.create(time_delta_params)
redirect_to stock_path(#stock)
end
private
def time_delta_params
params.require(:time_delta).permit(:start, :length)
end
end
Show for the stock
<h1> Stock </h1>
<table>
<tr>
<th>Stock</th>
<th>Hashtag</th>
</tr>
<tr>
<td><%= #stock.name %></td>
<td><%= #stock.hashtag %></td>
</tr>
</table>
<h3>TimeDeltas: </h2>
<table>
<tr>
<th>Start</th>
<th>Length</th>
</tr>
<% #stock.time_deltas.each do |time_delta| %>
<tr>
<td><%= #time_delta.start %></td>
<td><%= #time_delta.length %></td>
</tr>
<% end %>
</table>
<h3>Add a TimeDelta:</h2>
<%= form_for([#stock, #stock.time_deltas.build]) do |f| %>
<p>
<%= f.label :start %><br>
<%= f.text_field :start %>
</p>
<p>
<%= f.label :length %><br>
<%= f.text_field :length %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', stocks_path%>
<%= link_to 'Edit', edit_stock_path(#stock)%>
Any help is appreciated, thank you!
just remove # from time_delta
<% #stock.time_deltas.each do |time_delta| %>
<tr>
<td><%= time_delta.start %></td>
<td><%= time_delta.length %></td>
</tr>
<% end %>
up
You need # only to be able to share this var with your view. Eg: If you add this to your show action: #time_deltas = TimeDelta.all
you can show time_deltas in your view.
like:
<% #time_deltas.each do |td|%>
<%= td.start%>
<% end %>
I'm trying to paginate multiple tables with the Kaminari Gem. I've tried to do this by generating an array in my application controller, like so:
#paginate = %w(errors messages subscribers).page(params[:page]).per(15)
I've then taken #paginate and used it in one of my partials than renders the tables, like so:
<%= paginate #paginate %>
But I keep receiving a:
NoMethodError in Admin::ApplicationController#index
undefined method `page' for ["errors", "messages", "subscribers"]:Array
Here is the code that I've got:
Application_Controller.rb
class Admin::ApplicationController < InheritedResources::Base
protect_from_forgery
include ResourcesHelper
layout "admin"
#Setup
before_filter :set_resource_variable
before_filter :set_pagination_variable, only: :index
#Authentication
skip_before_filter :authenticate_user!
before_filter :authenticate_admin!
#Authorization
skip_before_filter :check_authentication
#Index
#Custom Index For Application/Index (no inheritance)
def index
#users = User.all
#attributes = %w(messages subscribers)
#paginate = %w(errors messages subscribers).page(params[:page]).per(15)
end
#Create
def create
create! { collection_path }
end
#Update
def update
update! { collection_path }
end
private
#Set Model Variable
def set_resource_variable
#resource = self.resource_class
#model = "#{#resource}".downcase.to_sym
end
#Pagination
def set_pagination_variable
#params[:page] ||= "1"
end
#Strong Params
def permitted_params
attributes = attributes(#resource) + %w(user_id admin_id) + [image_pages_attributes: [:caption, image_attributes: [:image]]]
params.permit(#model => attributes)
end
end
_table.html.erb
<h2><%= model %></h2>
<% unless collection.blank? %>
<table class="sort">
<thead>
<tr>
<% model.attribute_names.each do |attr| %>
<th><%= model.human_attribute_name(attr) %></th>
<% end %>
<th colspan="2"> </th>
<% if model.name == "Error" %><th> </th><% end %>
</tr>
</thead>
<tbody>
<%= render partial: "admin/resources/table/row", collection: collection, as: :resource, locals: {model: model} %>
</tbody>
</table>
_row.html.erb
<tr data-link="<%= polymorphic_path([:admin, resource]) %>">
<% model.attribute_names.each do |attr| %>
<td><%= resource.send(attr) %></td>
<% end %>
<% if model.name == "Error" %>
<td><%= link_to "Read", admin_error_read_path(resource.id), method: :put, remote: :true, class: "read" %></td>
<% end %>
<td><%= link_to "Edit", edit_polymorphic_path([:admin, resource]) %></td>
<td><%= link_to "Delete", polymorphic_path([:admin, resource]), method: :delete, data: {confirm: "Are you sure?"} %></td>
If you need anything else, let me know and I'll put it up.
Thank you!!
You're not setting "page" properly. It should always be like this:
paginate(:page => params[:page], :per_page => 15)
So in your example
#paginate = %w(errors messages subscribers).page(params[:page]).per(15)
What you really want is:
#paginate = %w(errors messages subscribers).paginate(:page => params[:page], :per_page => 15)