Models:
Store - id, name
Source - id, name
User - id, name
StoreUser - store_id, user_id
StoreSourceDeduction - store_id, source_id
StoreSourceRevenue - store_id, source_id
Transaction - store_id, source_id, deduction:boolean, user_id, amount, submitted_at, created_at, updated_at
(StoreSource split into Deduction and Revenue to make use of the Nested Form Fields gem)
I have routes set up as such: /transactions/:store_id/:date
When navigating to /transactions/1/08112017, if there are no records in the Transaction model for that date, I need to create a row in the Transaction table for each of the StoreSourceDeduction and StoreSourceRevenue models for store 1 and display a form for each record's amount field.
From the Transaction controller, how do I create multiple new Transaction objects while automatically populating the store_id, user_id, deduction, source_id fields, and how do I recall them to allow edits?
EDIT
I believe I solved the creation of objects:
class TransactionsController < ApplicationController
before_action :set_transactions, only: [:show, :edit, :update, :destroy]
before_action :set_store, only: [:store]
before_action :get_sources, only: [:show]
before_action :check_date, only: [:show]
# GET /transactions
# GET /transactions.json
def index
#transactions = Transaction.all
end
def store
end
# GET /transactions/1
# GET /transactions/1.json
def show
if is_today?
if #transactions.count == 0
#deductions.each do |d|
row = Transaction.new(:store_id => params[:store_id],
:source_id => d.source_id,
:user_id => current_user.id,
:deduction => true)
row.save
end
#revenues.each do |r|
row = Transaction.new(:store_id => params[:store_id],
:source_id => r.source_id,
:user_id => current_user.id,
:deduction => false)
row.save
end
end
end
end
# GET /transactions/1/edit
def edit
end
# POST /transactions
# POST /transactions.json
def create
end
# PATCH/PUT /transactions/1
# PATCH/PUT /transactions/1.json
def update
respond_to do |format|
if #transaction.update(transaction_params)
format.html { redirect_to #transaction, notice: 'Transaction was successfully updated.' }
format.json { render :show, status: :ok, location: #transaction }
else
format.html { render :edit }
format.json { render json: #transaction.errors, status: :unprocessable_entity }
end
end
end
# DELETE /transactions/1
# DELETE /transactions/1.json
def destroy
#transaction.destroy
respond_to do |format|
format.html { redirect_to transactions_url, notice: 'Transaction was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_transactions
#transactions = Transaction.where("store_id = :store_id AND date = :date",
{store_id: params[:store_id], date: params[:transaction_date]})
end
def set_store
#transactions = Transaction.where("store_id = :store_id",
{store_id: params[:store_id]})
end
def is_today?
date = params[:transaction_date]
today = Date.today().strftime('%m%d%Y')
date == today
end
def check_date
#date = Transaction.where("date = :date", {date: params[:date]})
end
def get_sources
#deductions = StoreSourceDeduction.where("store_id = :store_id", {store_id: params[:store_id]})
#revenues = StoreSourceRevenue.where("store_id = :store_id", {store_id: params[:store_id]})
end
# Never trust parameters from the scary internet, only allow the white list through.
def transaction_params
params.require(:transaction).permit(:store_id, :user_id, :source_id, :amount, :submitted_at)
end
end
Related
My OrdersController is as follows below, but I keep getting this message:
undefined method `listing_id=' for #
Extracted source (around line #31):
29
30
31 #order.listing_id = #listing.id
Is there something I am doing incorrectly? Am following a tutorial so followed the instructions, then when it wasn't working decided to copy and paste, and it's still not working. Please, any help is appreciated.
Full code is as follows
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def sales
#orders = Order.all.where(seller: current_user).order("created_at DESC")
end
def purchases
#orders = Order.all.where(buyer: current_user).order("created_at DESC")
end
# GET /orders/new
def new
#order = Order.new
#listing = Listing.find(params[:listing_id])
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
#listing = Listing.find(params[:listing_id])
#seller = #listing.user
#order.listing_id = #listing.id
#order.buyer_id = current_user.id
#order.seller_id = #seller.id
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => (#listing.price * 100).floor,
:currency => "usd",
:card => token
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
respond_to do |format|
if #order.save
format.html { redirect_to root_url, notice: 'Order was successfully created.' }
format.json { render action: 'show', status: :created, location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
#order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:address, :city, :state)
end
end
Check Fields of the Order Model. Do you have listing_id as a column on the orders table ? Check the migration files to make sure that somewhere along the way you have added a "listing_id" field to the "orders" table.
Define strong params:
def order_params
params.require(:order).permit(:address, :city, :state, :listing_id, :buyer_id, :seller_id )
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm doing a system of advanced searches, however I am having no results, the search is simply not working, I'm new to rails and never made an application of this.
my model
class Product < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
belongs_to :category
belongs_to :user
def self.search(params)
conditions_query = []
conditions_value = []
if params[:title].present?
conditions_query << 'title LIKE ?'
conditions_value << "%#{params[:title]}%"
end
if params[:category].present?
conditions_query << 'category LIKE ?'
conditions_value << "%#{params[:category]}%"
end
if params[:price].present?
conditions_query << 'price LIKE ?'
conditions_value << "%#{params[:price]}%"
end
conditions = [conditions_query.join(" AND "), conditions_value]
end
end
my controller:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :set_user, only: [:show, :edit, :update, :destroy, :create, :update, :new]
# GET /products
# GET /products.json
def index
#products = Product.all
#user = User.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
#product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
#product.update(user_id: params[:user_id])
format.html { redirect_to user_products_path(#user.id ,params[:product_id]), notice: 'Product was successfully created.' }
format.json { render 'show', status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to user_product_path, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to user_products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :price, :local, :description, :category_id, :contacts, :image, :user_id)
end
def set_user
#user = User.find(params[:user_id])
end
end
my application:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_user
helper_method :cate
def cate
Category.all
end
private
def current_user
User.find_by id: session[:user_id] if session[:user_id] if session[:user_id]
end
end
It appears you are on the right track, but the search method does not actually perform any query, even though you have built all the conditions. With a small adjustment to the last line you can end up with this:
def self.search(params)
conditions_query = []
conditions_value = []
if params[:title].present?
conditions_query << 'title LIKE ?'
conditions_value << "%#{params[:title]}%"
end
if params[:category].present?
conditions_query << 'category LIKE ?'
conditions_value << "%#{params[:category]}%"
end
if params[:price].present?
conditions_query << 'price LIKE ?'
conditions_value << "%#{params[:price]}%"
end
where(conditions_query.join(" AND "), *conditions_value)
end
You were missing the where, and you should splat out the conditions_value array.
Though I would argue a better way to write this out would be with scopes like so:
scope :title_like, -> (title) { where('title LIKE ?', "%#{title}%") if title.present? }
scope :category_like, -> (category) { where('category LIKE ?', "%#{category}%") if category.present? }
scope :price_like, -> (price) { where('price LIKE ?', "%#{price}%") if price.present? }
def self.search(params)
title_like(params[:title])
.category_like(params[:category])
.price_like(params[:price])
end
I'm trying to add a line to my orders_items controller so if its a new order to increment the counter from zero to one. so have created an action that does that before the save is called but when I try it I get:
undefined method `+' for nil:NilClass
def create
#order_item = #order.order_items.find_or_initialize_by_product_id(params[:product_id])
# Error below
#order_item.quantity += 1
respond_to do |format|
if #order_item.save
end
end
order_item.rb:
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
validates :order_id, :product, presence: true
validates :quantity, numericality: { only_integer: true, greater_than: 0 }
def subtotal
quantity * product.price
end
end
order_items_controller.rb:
class OrderItemsController < ApplicationController
before_action :set_order_item, only: [:show, :edit, :destroy]
before_action :load_order, only: [:create]
# GET /order_items
# GET /order_items.json
def index
#order_items = OrderItem.all
end
# GET /order_items/new
def new
#order_item = OrderItem.new
end
# POST /order_items
# POST /order_items.json
def create
#order_item = #order.order_items.find_or_initialize_by_product_id(params[:product_id])
#order_item.quantity += 1
respond_to do |format|
if #order_item.save
format.html { redirect_to #order, notice: 'Successfully added product to cart.' }
format.json { render action: 'show', status: :created, location: #order_item }
else
format.html { render action: 'new' }
format.json { render json: #order_item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /order_items/1
# PATCH/PUT /order_items/1.json
def update
#order_item = OrderItem.find(params[:id])
respond_to do |format|
if order_item_params[:quantity].to_i == 0
#order_item.destroy
format.html { redirect_to #order_item.order, notice: 'Order item was successfully updated.' }
format.json { head :no_content }
elsif #order_item.update(order_item_params)
format.html { redirect_to #order_item.order, notice: 'Successfully updated the order item.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #order_item.errors, status: :unprocessable_entity}
end
end
end
# DELETE /order_items/1
# DELETE /order_items/1.json
def destroy
#order_item.destroy
respond_to do |format|
format.html { redirect_to #order_item.order }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def load_order
#order = Order.find_or_initialize_by_id(session[:order_id], status: "unsubmitted")
if #order.new_record?
#order.save!
session[:order_id] = #order.id
end
end
def set_order_item
#order_item = OrderItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_item_params
params.require(:order_item).permit(:product_id, :order_id, :quantity)
end
end
Migration file:
class AddDefaultQuantityToOrderItems < ActiveRecord::Migration
def change
change_column :order_items, :quantity, :integer, default: 0
end
end
rails console:
#order.order_items.find_or_initialize_by_product_id(params[:product_id])
NoMethodError: undefined method `order_items' for nil:NilClass
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Model order.rb:
class Order < ActiveRecord::Base
has_many :order_items, dependent: :destroy
def total
order_items.map(&:subtotal).sum
end
end
orders_controller.rb:
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
#orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
end
# GET /orders/new
def new
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render action: 'show', status: :created, location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to products_path }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
#order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:user_id, :status)
end
end
Update
Rake routes shows route call for /:product_id
product.rb
class Product < ActiveRecord::Base
validates_numericality_of :price
validates :stock ,numericality: { greater_than_or_equal_to: 0 }
end
order.rb
# #Chiperific added proper 4-line indentation for SO viewing.
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
validates :order_id, :product, presence: true
validates :quantity, numericality: { only_integer: true, greater_than: 0 }
def subtotal
quantity * product.price
end
end
order items.rb
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
validates :order_id, :product, presence: true
validates :quantity, numericality: { only_integer: true, greater_than: 0 }
def subtotal
quantity * product.price
end
end
when i add
def create
#order_item = OrderItems.find_or_initialize_by_product_id(params[:product_id])
#order_item.quantity +=1
i get NameError in OrderItemsController#create - uninitialized constant OrderItemsController::Orderitems
def create
#order_item = Orderitems.find_or_initialize_by_product_id(params[:product_id]) <-error
#order_item.quantity += 1
if i try
#order = Order.find(params[:order_id])
#order_item = #order.order_items.find_or_initialize_by_product_id(params[:product_id])
#order_item.quantity += 1
i get ActiveRecord::RecordNotFound in OrderItemsController#create - Couldn't find Order without an ID
def create
#order = Order.find(params[:order_id])
#order_item = #order.order_items.find_or_initialize_by_product_id(params[:product_id])<-error
#order_item.quantity += 1
I don' t see where #order is defined. Since #order is null, #order_items will be as well.
I'm assuming your view URL is something like /orders/:order_id/order_items/:id
So let's use the URL parameter to make sure you get an Order or Order_ID initiated.
Option 1: Don't worry about #order
def create
#order_item = OrderItem.find_or_initialize_by_product_id(params[:product_id])
#order_item.quantity +=1
...
end
Option 2: Set the `#order` first
def create
#order = Order.find(params[:order_id])
#order_item = #order.order_items.find_or_initialize_by_product_id(params[:product_id])
#order_item.quantity += 1
...
end
Other potential problem:
.find_or_initialize_by_product_id(params[:product_id]) isn't actually finding or initializing.
It looks like this is your table relationship:
[Orders]`````\
|--> [Order_Items]
[Products]___/
I don't see where you have accepts_nested_attributes_for :order_items in orders.rb
You probably also need accepts_nested_attributes_for :order_items in products.rb
Does an Order_Item record have a Product_id field? I see where you're validating the presence of :product, but not :product_id, what does your schema look like? What happens if you try `find__or_initialize_by_product(params[:product_id])
Does your route call for /:product_id or /:product or neither? (a.k.a. is there a params[:product_id]?)
Update:
Thanks for the updated comments. Please add the whole URI being used, the whole Rake Routes line, or the Routes.rb file.
Or, while I know it's not SO-best-practice, you could post a link to the project on Github and I'll take a look there.
I'm still convinced that (at least one of) the issue(s) is that #order is not being set.
Trying it out in the Rails console is a great way to find out of .find_or_initialize_by_product_id is working
But, rails console can’t pull params and you still aren’t assigning #order first.
To test, in the rails console, try:
#order = Order.first #<----(just so you get #order assigned)
#order.order_items.find_or_initialize_by_product_id(1)
Also:
#order_item = OrderItems.find_or_initialize_by_product_id(params[:product_id])
OrderItems should be singular (my bad!!):
#order_item = OrderItem.find_or_initialize_by_product_id(params[:product_id])
I'll pay closer attention to this post and be more timely with my posts as I'm sure you're getting frustrated. Hang in there, your methodology is good.
But, there may be another way if we can't get these kinks worked out. Could we find the order item quantity by counting the # of records with a specific order id instead of saving the number statically in the database? Just a thought if you're ready to bail on this problem.
You can initialize #order_item.quantity by putting this in your OrderItem class:
def quantity
self.quantity ||= 0
end
Go into your method find_or_initialize_by_product_id, and make sure you initialize the attribute quantity to 0.
Here's a simple reproduction of your error - notice I never initialized x to 0:
$ irb
irb(main):001:0> x += 5
NoMethodError: undefined method `+' for nil:NilClass
from (irb):1
from /usr/bin/irb:11:in `<main>'
In Rails 4 you can use the following:
#order_item = #order.order_items.where(product_id: params[:product_id]).first_or_create
instead of
#order_item = #order.order_items.find_or_initialize_by_product_id(params[:product_id])
and this:
#order = Order.where(session[:order_id], status: "unsubmitted").first_or_initialize
instead of
#order = Order.find_or_initialize_by_id(session[:order_id], status: "unsubmitted")
I want to edit a has_many through relation, but instead of editing the relation, it creates a new model.
In my form:
<%= form_for #service do |f| %>
<%= f.fields_for :service_users do |ac| %>
<% end %>
<% end %>
In my model:
class Service < ActiveRecord::Base
has_many :service_users
has_many :users, :through => :service_users
accepts_nested_attributes_for :service_users
end
Begin situation:
When i update the comments field:
After updating i see the edited relation as a duplication of the first one.
In some way i have to check if there're already relations present, but how?
Update:
My controller:
class ServicesController < ApplicationController
before_action :set_service, only: [:show, :edit, :update, :destroy, :users]
before_filter :authenticate_user!
# GET /services
# GET /services.json
def index
services = current_user.available_services
#available_services = services.group_by { |t| t.date.beginning_of_month }
end
# GET /services/1
# GET /services/1.json
def show
# service_users = current_user.service_users
#
# Service.find_each do |service|
# unless service_users.detect { |m| m.service_id == service.id }
# current_user.service_users.build service_id: service.id
# end
# end
#
#available_users = #service.available_users.group_by { |u| u.group }
#planned_users = #service.planned_users.group_by { |u| u.group }
#reserve_users = #service.reserve_users.group_by { |u| u.group }
end
# GET /services/new
def new
#service = Service.new
end
# GET /services/1/edit
def edit
#service.service_users.create
end
# POST /services
# POST /services.json
def create
#service = Service.new(service_params)
p service_params
respond_to do |format|
if #service.save
format.html { redirect_to #service, notice: 'Service was successfully created.' }
format.json { render action: 'show', status: :created, location: #service }
else
format.html { render action: 'new' }
format.json { render json: #service.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /services/1
# PATCH/PUT /services/1.json
def update
respond_to do |format|
p service_params[:service_users_attributes]
if #service.update(service_params)
format.html { redirect_to #service, notice: 'Service was successfully updated.' }
format.json { render action: 'show', status: :ok, location: #service }
else
format.html { render action: 'edit' }
format.json { render json: #service.errors, status: :unprocessable_entity }
end
end
end
def users
#users = #service.users
end
# DELETE /services/1
# DELETE /services/1.json
def destroy
#service.destroy
respond_to do |format|
format.html { redirect_to services_url }
format.json { head :no_content }
end
end
def destroy_association
if params[:id].present?
ServiceUser.find(params[:id]).delete
redirect_to root_path
end
end
def make_user_available_for_service
p '########'
p params
p #service
redirect_to root_path
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service
#service = Service.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_params
params.require(:service).permit(:date, :comments,
service_users_attributes: [:user_id,
:service_id,
:availability,
:comments],
service_groups_attributes: [:service_id,
:group_id,
:start_time,
:end_time])
end
end
Try build instead of create.
Like this:
def edit
#service.service_users.build
end
And check your service_params.
Your missing an id for the service_users.
We are trying to insert an object into our rails controller (terminology may be wrong i'm new to ruby and rails) that has objects which contain objects. For just the basic portion we are trying to insert a location that has a restaurant, so the restaurant has an id that refers to the location. Ideally we'd like it to only insert the location if it doesn't exist and give it the id after insert, if it already exists just fill in the id that already exists. Right now we are inserting the location but the restaurants insert never happens.
Heres the sample json object we are trying to insert, it also has the other controllers we are trying to insert into. Location has one restaurant that has many inspections that has many violations. Tables are (locations, restaurants, inspections, violations)
{
"locations": {
"restaurants_attributes": {
"name": "CORNERSTONE GROUP HOME",
"type": "INSTITUTION",
"inspections_attributes": {
"date": "2013-11-19",
"number": "26134",
"violations_attributes": {
"comment": "WOODEN SHELVING IN DISREPAIR",
"code": "4-501.11",
"critical": "0"
}
}
},
"st_apt_num": "",
"st_dir": "E",
"st_number": "1250",
"st_suffix": "RD",
"zip_code": "65201",
"latitude": 38.9808446,
"longitude": -92.289225
}
}
Here is the models for locations / restaurants
class Location < ActiveRecord::Base
has_one :restaurants
accepts_nested_attributes_for :restaurants
end
class Restaurant < ActiveRecord::Base
belongs_to :locations
has_many :inspections
accepts_nested_attributes_for :inspections
end
Here is the migrate for locations / restaurants
class CreateLocations < ActiveRecord::Migration
def change
create_table :locations do |t|
t.integer :st_apt_num
t.string :st_dir
t.integer :st_number
t.string :st_suffix
t.integer :zip_code
t.decimal :latitude, :precision => 9, :scale => 7
t.decimal :longitude, :precision => 9, :scale => 7
t.timestamps
end
end
end
class CreateRestaurants < ActiveRecord::Migration
def change
create_table :restaurants do |t|
t.belongs_to :locations
t.string :name
t.string :type
t.timestamps
end
end
end
Here is the controllers for locations / restaurants (haven't changed anything here though)
class LocationsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_location, only: [:show, :edit, :update, :destroy]
# GET /locations
# GET /locations.json
def index
#locations = Location.all
end
# GET /locations/1
# GET /locations/1.json
def show
end
# GET /locations/new
def new
#location = Location.new
end
# GET /locations/1/edit
def edit
end
# POST /locations
# POST /locations.json
def create
#location = Location.new(location_params)
respond_to do |format|
if #location.save
format.html { redirect_to #location, notice: 'Location was successfully created.' }
format.json { render action: 'show', status: :created, location: #location }
else
format.html { render action: 'new' }
format.json { render json: #location.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /locations/1
# PATCH/PUT /locations/1.json
def update
respond_to do |format|
if #location.update(location_params)
format.html { redirect_to #location, notice: 'Location was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #location.errors, status: :unprocessable_entity }
end
end
end
# DELETE /locations/1
# DELETE /locations/1.json
def destroy
#location.destroy
respond_to do |format|
format.html { redirect_to locations_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_location
#location = Location.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def location_params
params.require(:locations).permit(:st_apt_num, :st_dir, :st_number, :st_suffix, :zip_code, :latitude, :longitude)
end
end
class RestaurantsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_restaurant, only: [:show, :edit, :update, :destroy]
# GET /restaurants
# GET /restaurants.json
def index
#restaurants = Restaurant.all
end
# GET /restaurants/1
# GET /restaurants/1.json
def show
end
# GET /restaurants/new
def new
#restaurant = Restaurant.new
end
# GET /restaurants/1/edit
def edit
end
# POST /restaurants
# POST /restaurants.json
def create
#restaurant = Restaurant.new(restaurant_params)
respond_to do |format|
if #restaurant.save
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully created.' }
format.json { render action: 'show', status: :created, location: #restaurant }
else
format.html { render action: 'new' }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /restaurants/1
# PATCH/PUT /restaurants/1.json
def update
respond_to do |format|
if #restaurant.update(restaurant_params)
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /restaurants/1
# DELETE /restaurants/1.json
def destroy
#restaurant.destroy
respond_to do |format|
format.html { redirect_to restaurants_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_restaurant
#restaurant = Restaurant.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def restaurant_params
params.require(:restaurant).permit(:name, :loc_id, :type)
end
end