I'm writing a simple rails app which has users,projects and tasks.
And I have the next problem: each user must have his own projects,and each projects must have it's own tasks.When I'm trying to debug my app via console I can see that projects and tasks are created without "user_id" and "project_id".So my DB was configurated incorrectly and because of app doesn't work correctly(columns weren't add to Projects and Tasks tables and because of it app simply can't create projects,that will belong to only one user and tasks which belong to only one project.(and that's the main point of the app)
Can someone help me to deal with my models and migrations,cause I'me really stuck with this stuff
Here are the corresponding models and migrations
Project.rb
class Project < ActiveRecord::Base
belongs_to :user
has_many :tasks, dependent: :destroy
validates :name, presence: true, uniqueness: true
end
User.rb
class User < ActiveRecord::Base
has_many :tasks, through: :projects, dependent: :destroy
has_many :projects, dependent: :destroy
attr_accessor :remember_token
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Returns true if the given token matches the digest.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
# Forgets a user
def forget
update_attribute(:remember_digest, nil)
end
end
Task.rb
class Task < ActiveRecord::Base
belongs_to :project
end
Migration for creating projects
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.timestamps null: false
end
end
end
And for creating tasks
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :name
t.datetime :deadline
t.timestamps null: false
end
end
end
Now when I try to create/update/delete a new task I always get some errors like
Also here are the corresponding controllers,maybe something also wrong there
projects_controller.rb
class ProjectsController < ApplicationController
before_action :find_project!, only: [ :update, :destroy]
# GET /projects
# GET /projects.json
def index
#projects = current_user.projects
#project = Project.new
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
#project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
#project = current_user.projects.create(project_params)
respond_to do |format|
if #project.save
format.html { redirect_to home_url }
format.json { render :show, status: :created, location: #project }
else
format.html { render :home_url }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to home_url }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :home_url }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
#project.destroy
respond_to do |format|
format.html { redirect_to home_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions
def find_project!
#project = current_user.projects.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name)
end
end
tasks_controller.rb
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
end
# GET /tasks/1
# GET /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = #project.tasks.create(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to home_url }
format.json { render :show, status: :created, location: #task }
else
format.html { render :home_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to home_url }
format.json { render :home_url, status: :ok, location: #task }
else
format.html { render :home_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
current_user.tasks.where(id: params[:task][:task_ids])
respond_to do |format|
format.html { redirect_to home_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def task_params
params.require(:task).permit(:name, :position)
end
def find_task!
#task = current_user.tasks.find(params[:id])
end
def find_project!
#project = current_user.projects.find(params[:task][:project_id])
end
end
Add the belongs to in the migrations as well.
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.belongs_to :user
t.timestamps null: false
end
end
end
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :name
t.datetime :deadline
t.belongs_to :project
t.timestamps null: false
end
end
end
Related
I'm creating a task manager using Ruby on Rails.
My task manager has a Task model table that includes a column called duedate.
I need to be able to find all tasks that are overdue.
How can I write a function that automatically allows me to mark all overdue tasks to True?
This is my Schema.
create_table "tasks", force: :cascade do |t|
t.string "title"
t.string "description"
t.datetime "duedate"
t.boolean "completed"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
my tasks controller looks like this
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy ]
# before_action :due_today, only: %i[ index show]
# GET /tasks or /tasks.json
def index
#tasks = Task.all.overdue.order(duedate: :asc)
end
# GET /tasks/1 or /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks or /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: "Task was successfully created." }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: "Task was successfully updated." }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1 or /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
format.json { head :no_content }
end
end
def overdue?
overdue < Time.now
end
# def overdue
# #tasks = Task.where(overdue: true)
# end
# def due_today
# #tasks = Task.where(duedate: = Date.now)
# end
private
# Use callbacks to share common setup or constraints between actions.
# def overdue?
# if(DateTime.now > duedate)
# #task.overdue = true
# else
# #task.overdue = false
# end
# end
# def overdue
# #tasks = Task.where(overdue: true)
# end
def set_task
#task = Task.find(params[:id])
end
# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:title, :description, :duedate, :completed, :overdue)
end
end
my model looks like this
class Task < ApplicationRecord
scope :overdue, -> { where("duedate < ?", Time.now) }
def overdue?
overdue < Time.now
end
end
I suggest removing the overdue column from your database. It is not useful because the comparison for the duedate column to the current time is more precise and doesn't depend on the manual setting of a boolean column.
You can use a scope to find all records that are overdue:
# in your model at app/models/task.rb
scope :overdue, -> { where("duedate < ?", Time.now) }
# in your controller
def index
#tasks = Task.overdue.order(:duedate)
end
When you want to have an overdue? method on your model, for example, to show an overdue flag in the view then add an overdue? method to your model:
# in your model at app/models/task.rb
def overdue?
duedate < Time.now
end
I am working on a wiki application in Rails that would be publicly editable. I have an articles controller and a drafts controller. When someone clicks 'edit' on an article, I would like to create a new draft with the contents of the original article, and then save that to the database table when the user clicks 'save'. Any ideas on how I might go about doing this? I've been stuck on it for a few days.
Currently, each article belongs_to a category, a subcategory, and has_many drafts.
Database schema:
ActiveRecord::Schema.define(version: 20160723153357) do
create_table "articles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "content"
t.integer "category_id"
t.integer "subcategory_id"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "drafts", force: :cascade do |t|
t.string "title"
t.text "content"
t.integer "category_id"
t.integer "subcategory_id"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "subcategories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
end
Articles_controller:
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
# GET /articles
# GET /articles.json
def index
if params[:category].blank? && params[:subcategory].blank?
#articles = Article.all.order("created_at DESC")
elsif params[:subcategory].blank?
#category_id = Category.find_by(name: params[:category]).id
#articles = Article.where(category_id: #category_id).order("created_at DESC")
else
#subcategory_id = Subcategory.find_by(name: params[:subcategory]).id
#articles = Article.where(subcategory_id: #subcategory_id).order("created_at DESC")
end
end
# GET /articles/1
# GET /articles/1.json
def show
end
# GET /articles/new
def new
#article = Article.new
end
# GET /articles/1/edit
def edit
end
# POST /articles
# POST /articles.json
def create
#parameters = article_params
#parameters[:category] = Category.find_by(id: Subcategory.find_by(id: article_params[:subcategory_id]).category_id)
#article = Article.new(#parameters)
respond_to do |format|
if #article.save
format.html { redirect_to #article, notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: #article }
else
format.html { render :new }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /articles/1
# PATCH/PUT /articles/1.json
def update
respond_to do |format|
if #article.update(article_params)
format.html { redirect_to #article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: #article }
else
format.html { render :edit }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.json
def destroy
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
#article = Article.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:title,:content,:subcategory_id)
end
end
Drafts_controller:
class DraftsController < ApplicationController
before_action :set_draft, only: [:show, :edit, :update, :destroy]
# GET /drafts
# GET /drafts.json
def index
#drafts = Draft.all
end
# GET /drafts/1
# GET /drafts/1.json
def show
end
# GET /drafts/new
def new
#draft = Draft.new
end
# GET /drafts/1/edit
def edit
end
# POST /drafts
# POST /drafts.json
def create
#parameters = draft_params
#parameters[:article_id] = params[:article_id]
#parameters[:subcategory_id] = 2
#parameters[:category_id] = 2
#draft = Draft.new(#parameters)
respond_to do |format|
if #draft.save
format.html { redirect_to #draft, notice: 'Draft was successfully created.' }
format.json { render :show, status: :created, location: #draft }
else
format.html { render :new }
format.json { render json: #draft.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /drafts/1
# PATCH/PUT /drafts/1.json
def update
respond_to do |format|
if #draft.update(draft_params)
format.html { redirect_to #draft, notice: 'Draft was successfully updated.' }
format.json { render :show, status: :ok, location: #draft }
else
format.html { render :edit }
format.json { render json: #draft.errors, status: :unprocessable_entity }
end
end
end
# DELETE /drafts/1
# DELETE /drafts/1.json
def destroy
#draft.destroy
respond_to do |format|
format.html { redirect_to drafts_url, notice: 'Draft was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_draft
#draft = Draft.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def draft_params
params.require(:draft).permit(:title, :content)
end
end
Article model:
class Article < ApplicationRecord
belongs_to :category
belongs_to :subcategory
has_many :drafts
end
Draft model:
class Draft < ApplicationRecord
belongs_to :category
belongs_to :subcategory
belongs_to :article
end
I think an approach I would probably follow would be to extract the content information to another table altogether. Here's a rough implementation I could come up with immediately:
class Article < ApplicationRecord
#column_names: ['type:string']
has_many :contents
has_one :current_content, -> { current.or(approved) }, class_name: 'Content'
delegate :title, :content, to: :current_content, allow_nil: true
end
class Content < ApplicationRecord
#column_names: ["article_id:int", "title:string", "content:text", "status:int"]
belongs_to :article
enum status: [:unapproved, :approved, :current]
end
class Draft < Article
#use STI here
end
#services/set_current_article_content.rb
class SetCurrentArticleContent
attr_reader :article, :content
def initialize(article, content)
#article = article
#content = content
end
def call
article.current_content.approved!
content.current!
end
end
#services/edit_wiki_content.rb
class EditWikiContent.rb
attr_reader :article, :content
def initialize(article, content)
#article = article
#content = content
end
def call
article.contents << content
content.save!
end
end
#services/publish_draft.rb
class PublishDraft
attr_reader :draft
def initialize(draft)
#draft = draft
end
def call
draft.becomes!(Article)
end
end
There are three services which would handle the updating and setting of the current content and also publishing the draft, you could add some additional logic in any of them. Also note the condition for the current_content in the article model, your logic might be different from the way I have implemented it.
Hey I'm doing something strange. Using both Paperclip & Carrierwave to determine which i like better because I am new to rails but here is the issue.
When I create a post using Paperclip, the image gets routed to the directory but does not display/render in any view.
When I create a post using Carrierwave, the image never gets routed or renders and I get an error withing the postscontroller saying.
(undefined method `image_will_change!' for #Post:0x007fd039c02888)
app/controllers/posts_controller.rb:27:in `create'
I also get an error across the app when trying to edit any of the post that error is.
(undefined method 'image_changed?' for #Post:0x007fd03fc76fe8)
app/controllers/posts_controller.rb:44:in `block in update'
app/controllers/posts_controller.rb:43:in `update'
Heres my code:
(post.rb)
class Post < ActiveRecord::Base
attr_accessor :image
has_attached_file :content, :styles => { :medium => "300x300>", :thumb => "100x100>" }
mount_uploader :image, ImageUploader
end
(postscontroller.rb)
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy, :destroy_all]
def index
#posts = Post.all
end
def show
end
def new
#post = Post.new
end
def edit
end
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content, :no_image }
end
end
def destroy_all
#post.destroy_all
respond_to do |format|
format.html { redirect_to posts, notice: 'All Posts have bee successfully destroyed.'}
format.json { head :no_content, :no_image}
end
end
def demo
#post = Post.all
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description, :content, :image)
end
end
(imageuploader.rb)
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"images"
end
(add_content_to_posts.rb)
class AddContentToPosts < ActiveRecord::Migration
def self.up
add_attachment :posts, :content
end
def self.down
remove_attachment :posts, :content
end
end
(add_image_to_posts.rb)
class AddImageToPosts < ActiveRecord::Migration
def change
add_column :posts, :images, :string
end
end
(Schema.rb)
create_table "posts", force: true do |t|
t.string "title"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "content_file_name"
t.string "content_content_type"
t.integer "content_file_size"
t.datetime "content_updated_at"
t.string "images"
end
I'm working on making a simple app that has users which can have many playlists. I'm trying to render the New view of a playlist but I'm getting this error:
NoMethodError in PlaylistsController#new
undefined method `playlist' for nil:NilClass
def new
#playlist = #user.playlist.new
end
Here's some context:
EDIT: I uploaded the relevant parts of my code to a gist.github:
https://gist.github.com/izikperz/164eab76e64d375d9075
Playlist_controller.rb
class PlaylistsController < ApplicationController
before_action :set_playlist, only: [:show, :edit, :update, :destroy]
:set_user
# GET /playlists
# GET /playlists.json
def index
#playlists = Playlist.all
end
# GET /playlists/1
# GET /playlists/1.json
def show
end
# GET /playlists/new
def new
#playlist = #user.playlist.new
end
# GET /playlists/1/edit
def edit
end
# POST /playlists
# POST /playlists.json
def create
#playlist = #user.playlists.new(playlist_params)
respond_to do |format|
if #playlist.save
format.html { redirect_to #user.playlist, notice: 'Playlist was successfully created.' }
format.json { render :show, status: :created, location: #playlist }
else
format.html { render :new }
#format.json { render json: #playlist.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /playlists/1
# PATCH/PUT /playlists/1.json
def update
respond_to do |format|
if #playlist.update(playlist_params)
format.html { redirect_to #playlist, notice: 'Playlist was successfully updated.' }
format.json { render :show, status: :ok, location: #playlist }
else
format.html { render :edit }
format.json { render json: #playlist.errors, status: :unprocessable_entity }
end
end
end
# DELETE /playlists/1
# DELETE /playlists/1.json
def destroy
#playlist.destroy
respond_to do |format|
format.html { redirect_to playlists_url, notice: 'Playlist was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_user
#user = User.find_by(params[:user_id])
end
# Use callbacks to share common setup or constraints between actions.
def set_playlist
#playlist = Playlist.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def playlist_params
params.require(:playlist).permit(:user_id, :title, :img)
end
end
in my routes.rb I have:
resources :users do
resources :playlists
end
My user.rb model:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
has_secure_password
validates :password, length: { minimum: 6 }
has_many :playlists
end
Playlist.rb model:
class Playlist < ActiveRecord::Base
belongs_to :user, inverse_of: :playlist
validates :user_id, presence: true
end
My database schema:
ActiveRecord::Schema.define(version: 20141105043809) do
create_table "playlists", force: true do |t|
t.string "title"
t.string "img"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", primary_key: "user_id", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "imgurl"
end
end
Anyone have any ideas?
The #user object in the new action is nil.
undefined method `playlist' for nil:NilClass
This is because params[:user_id] was not sent from your view.
Fix it by chaniging:
new_user_playlist_path(params[:user_id])
to:
new_user_playlist_path(user_id: params[:user_id])
After fixing that, another error will arise in the new action:
#playlist = #user.playlist.new
It should be pluralized as it is in your association:
#playlist = #user.playlists.new
#or
#playlist = #user.playlists.build
Or Simply ignore that:
#playlist = Playlist.new(user_id: #user.id)
Since the ActiveRecord Association between User and Playlist is :has_many and :belongs_to these are many to many associations. This implies that one user has more than one playlists. The association will provide you with a instance method #user.playlists not #user.playlist. Try to use the plural version. It should work.
Make sure that you are sending user_id along with form so that
#user = User.find_by(id: params[:user_id]) is not nil
and then change this
#playlist = #user.playlists.new
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