Rails Paperclip Carrierwave.. Errors - ruby-on-rails

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

Related

How to add more players to a game in Rails / Model relationship

I am being super new to Rails, so, please keep it in mind reading my post.
I am trying to develop a very simple app where I can create a game and assign players to this game.
So far I have a scaffolded game and player resources. I have set has_many :players and has_many: games relationship, for both, - game and player models.
I added form.select in view form (new):
<%= form.label :player_id %>
<%= form.select :player_id, Player.all.collect{|x| [x.name]} %>
What I am trying to achieve is, - I want to be able to add multiple players to the single game. If I paste the same form.select field again it will overwrite first selection and eventually save only one (last) player.
I can, however, replicate this functonality in rails console. with:
g = Game.last
g.players << Player.find(1)
g.players << Player.find(2)
And it works.
Can you advise me on how to do same at FronEnd level? I understand if my approach is completely wrong, then let me know what will be a better one.
Many thanks in advance.
Update 1.
I have very default scaffold controller:
class GamesController < ApplicationController
before_action :set_game, only: [:show, :edit, :update, :destroy]
def index
#games = Game.all
end
def show
end
def new
#game = Game.new
end
def edit
end
def create
#game = Game.new(game_params)
respond_to do |format|
if #game.save
format.html { redirect_to #game, notice: 'Game was successfully created.' }
format.json { render :show, status: :created, location: #game }
else
format.html { render :new }
format.json { render json: #game.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #game.update(game_params)
format.html { redirect_to #game, notice: 'Game was successfully updated.' }
format.json { render :show, status: :ok, location: #game }
else
format.html { render :edit }
format.json { render json: #game.errors, status: :unprocessable_entity }
end
end
end
def destroy
#game.destroy
respond_to do |format|
format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_game
#game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:name, :date, :player_id)
end
end
and my db tables look like this:
class CreatePlayers < ActiveRecord::Migration[5.1]
def change
create_table :players do |t|
t.string :name
t.string :role
t.integer :game_id
t.timestamps
end
end
end
class CreateGames < ActiveRecord::Migration[5.1]
def change
create_table :games do |t|
t.string :name
t.date :date
t.integer :player_id
t.timestamps
end
end
end
Do i need to have action in game controller for it ?

Return a value in a different Rails view

Running Rails:
Rails 4.2.5
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
I'm trying to return a value from one model (eds) within a view of another model (clients).
I believe I I'm very close to a solution. However, I'm at a road block. I'm want return the name of the school (eds) in the client view.
As of now, I'm defining this in my client controller:
def index
#eds_school = Ed.name
end
I thought that Ed.name should work but it doesn't.
Then I'm calling the statement in the view:
<tb><%= #eds_school %></td>
I think my issue is within the client controller.
Here is the db schema:
create_table "eds", force: :cascade do |t|
t.string "name"
t.string "grade"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "school"
t.text "JtText"
end
Eds Controller:
class EdsController < ApplicationController
before_action :set_ed, only: [:show, :edit, :update, :destroy]
# GET /eds
# GET /eds.json
def index
#eds = Ed.all
end
# GET /eds/1
# GET /eds/1.json
def show
end
# GET /eds/new
def new
#ed = Ed.new
end
# GET /eds/1/edit
def edit
end
# POST /eds
# POST /eds.json
def create
#ed = Ed.new(ed_params)
respond_to do |format|
if #ed.save
format.html { redirect_to #ed, notice: 'Ed was successfully created.' }
format.json { render :show, status: :created, location: #ed }
else
format.html { render :new }
format.json { render json: #ed.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /eds/1
# PATCH/PUT /eds/1.json
def update
respond_to do |format|
if #ed.update(ed_params)
format.html { redirect_to #ed, notice: 'Ed was successfully updated.' }
format.json { render :show, status: :ok, location: #ed }
else
format.html { render :edit }
format.json { render json: #ed.errors, status: :unprocessable_entity }
end
end
end
# DELETE /eds/1
# DELETE /eds/1.json
def destroy
#ed.destroy
respond_to do |format|
format.html { redirect_to eds_url, notice: 'Ed was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ed
#ed = Ed.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ed_params
params.require(:ed).permit(:name)
end
end
Client Controller:
class ClientsController < ApplicationController
before_action :authenticate_user!
before_action :set_client, only: [:show, :edit, :update, :destroy]
# GET /clients
# GET /clients.json
def index
#clients = Client.all.uniq.order("created_at DESC")
#clients_count = Client.uniq.count
#eds_school = Ed.name
end
# GET /clients/1
# GET /clients/1.json
def show
##notes = Note.all.uniq.order("created_at DESC")
#notes = Note.where(client_id: #client.id) #Where a note belong to the current user
end
# GET /clients/new
def new
#client = Client.new
end
# GET /clients/1/edit
def edit
end
# POST /clients
# POST /clients.json
def create
#client = Client.new(client_params)
respond_to do |format|
if #client.save
format.html { redirect_to #client, notice: 'Client was successfully created.' }
format.json { render :show, status: :created, location: #client }
else
format.html { render :new }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
#if params[:remove_image]
##client.remove_image!
#client.save
#end
respond_to do |format|
if #client.update(client_params)
format.html { redirect_to #client, notice: 'Client was successfully updated.' }
format.json { render :show, status: :ok, location: #client }
else
format.html { render :edit }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
#client.destroy
respond_to do |format|
format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client
#client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
params.require(:client).permit(:firstName, :lastName,:dob, :name, :gender_id, :RefbText, :JtText, :text_rs, :msub, :text_id, :ged_id, :mj_id, :od_id, :otc_id, :cigarette_id, :alcohol_id, :grad, :remove_image, :rh_options, :insurance_id, :state_id, :ed_id, :wk_id, :grade_id, :rsource_id, :image, :race_id, :employment_id, :comments, :clientemail, :phone, :truma_id, :college_id, :enrolled, :address, :city, :state, :zipcode, rhealth_ids:[], mhealth_ids:[], cparent_ids:[], preg_ids:[], referral_ids:[], refa_ids:[], refb_ids:[])
#params.require(:client).permit(:name, mhealth_ids:[])
end
end
Code Sample of Client Model:
class Client < ActiveRecord::Base
belongs_to :ed
end
Code Sample of Client DB Schema:
create_table "clients", force: :cascade do |t|
t.integer "ed_id"
end
Ed Controller:
class Ed < ActiveRecord::Base
has_many :clients
end
Its not completely clear what you're trying to achieve. Your code isnt working bcause name is an attribute and therefore a method on an instance of the class Eds. Look up the difference between class methods and instance methods. You need to find a particular instance of Eds and then call the name method on it e.g.
eds_school = Eds.find(1).name
where 1 is the database record in the eds table with an id of 1.
Because a client belongs to an Eds, you can access the eds name through a client instance. To display the eds.name for each client, you will need to loop through the clients variable. In the view
<% #clients.each do |client| %>
<%= client.eds.name %>
<% end %>

Rails Association Bug

I am getting NoMethodError in SitesController#index undefined method `name' for nil:NilClass in my Salesman index view and cannot find the culprit.
I have a simple rails app with the following tables: Customer, Salesman and Invoice.
In the index view for the customer table I have the following call:
<% #customers.each do |customer| %>
<%= customer.name %></td>
<%= customer.address %>
<%= customer.salesman.name %>
<% end %>
This is the call that results in the undefined method ´name´ listed above. I made sure the salesman table has a salesman_id foreign key in the customer table. Also, I made the respective association in the models:
class Customer < ActiveRecord::Base
belongs_to :salesman
has_many :invoices
end
class Salesman < ActiveRecord::Base
has_many :customers
end
I tested the customer.salesman.name call in the console and it works flawlessly. The show view for the customer has an exact call like this one and it also works. The only way I can make the customer index pass is by replacing customer.salesman.name to customer.salesman_id returning only the id.
Thank you for your time here.
***schema.rb***
ActiveRecord::Schema.define(version: 20150325172212) do
create_table "customers", force: :cascade do |t|
t.string "name"
t.string "address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "salesman_id"
end
create_table "invoices", force: :cascade do |t|
t.datetime "date"
t.string "fid"
t.integer "salesman_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "address"
end
create_table "salesmen", force: :cascade do |t|
t.datetime "name"
t.string "address""
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
***controllers***
**customer**
Class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy]
# GET /customers
# GET /customers.json
def index
#customers = Customer.all
end
# GET /customers/1
# GET /customers/1.json
def show
end
# GET /customers/new
def new
#customer = Customer.new
end
# GET /customers/1/edit
def edit
end
# POST /customers
# POST /customers.json
def create
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html { redirect_to #customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /customers/1
# PATCH/PUT /customers/1.json
def update
respond_to do |format|
if #customer.update(customer_params)
format.html { redirect_to #customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: #customer }
else
format.html { render :edit }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /customers/1
# DELETE /customers/1.json
def destroy
#customer.destroy
respond_to do |format|
format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cliente
#cliente = Cliente.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:name, :address, :salesman_id)
end
end
***salesman***
class SalesmenController < ApplicationController
before_action :set_salesman, only: [:show, :edit, :update, :destroy]
# GET /salesmans
# GET /salesmans.json
def index
#salesmen = Salesman.all
end
# GET /salesmans/1
# GET /salesmans/1.json
def show
end
# GET /salesmans/new
def new
#salesman = Salesman.new
end
# GET /salesmans/1/edit
def edit
end
# POST /salesmans
# POST /salesmans.json
def create
#salesman = Salesman.new(salesman_params)
respond_to do |format|
if #salesman.save
format.html { redirect_to #salesman, notice: 'Salesman was successfully created.' }
format.json { render :show, status: :created, location: #salesman }
else
format.html { render :new }
format.json { render json: #salesman.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /salesmans/1
# PATCH/PUT /salesmans/1.json
def update
respond_to do |format|
if #salesman.update(salesman_params)
format.html { redirect_to #salesman, notice: 'Salesman was successfully updated.' }
format.json { render :show, status: :ok, location: #salesman }
else
format.html { render :edit }
format.json { render json: #salesman.errors, status: :unprocessable_entity }
end
end
end
# DELETE /salesmans/1
# DELETE /salesmans/1.json
def destroy
#salesman.destroy
respond_to do |format|
format.html { redirect_to salesmans_url, notice: 'Salesman was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_salesman
#salesman = Salesman.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def salesman_params
params.require(:salesman).permit(:name, :address)
end
**invoice**
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
#invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
#invoice = Invoice.find(params[:id])
#ordenes = #invoice.ordenes
end
# GET /invoices/new
def new
#invoice = Invoice.new
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
#invoice = Invoice.new(invoice_params)
respond_to do |format|
if #invoice.save
format.html { redirect_to #invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: #invoice }
else
format.html { render :new }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if #invoice.update(invoice_params)
format.html { redirect_to #invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
#invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
#invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:date, :fid, :name, :address, :salesman_id)
end
end
-davefogo
Add the following to your views instead of what you currently have<%= customer.salesman.name if customer.salesman %>
This will ensure that you're not calling name when customer doesn't have a salesman.
Try this:
<% #customers.each do |customer| %>
<%= customer.name %>
<%= customer.address %>
<%= customer.salesman.try(:name) %>
<% end %>
Do this:
<% #customers.each do |customer| %>
<% Rails.logger.debug "\n\n#{#customers} has a nil customer in it\n\n" if customer.nil? %>
<% Rails.logger.debug "\n\nCustomer #{customer.id} has no salesman for [#{customer.salesman_id}]\n\n" if customer.salesman.nil? %>
<%= customer.name %>
<%= customer.address %>
<%= customer.salesman.name %>
<% end %>
...and then check your logs after hitting the index.

Rails Inserting Nested Object

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

Strong parameers throwing "Unknown key"Error

I am trying to get this model working with Strong Parameters but I can't seem to work out what I am doing wrong!?
ERROR:
Unknown key: name
Controller:
class PracticesController < ApplicationController
def practice_params
if params[:action] == 'create'
params.require(:practice).permit( :name, :billing_address, :physical_address, :phone_number, :fax_number, :emergency_contact, :emergency_phone, :email_addres, :active, :ABN, :time_zone)
elsif params[:action] == 'update'
params.require(:practice).permit( :name, :billing_address, :physical_address, :phone_number, :fax_number, :emergency_contact, :emergency_phone, :email_addres, :active, :ABN, :time_zone)
end
end
# GET /practices
# GET /practices.json
def index
#practices = Practice.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #practices }
end
end
# GET /practices/1
# GET /practices/1.json
def show
#practice = Practice.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #practice }
end
end
# GET /practices/new
# GET /practices/new.json
def new
#practice = Practice.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #practice }
end
end
# GET /practices/1/edit
def edit
#practice = Practice.find(params[:id])
end
# POST /practices
# POST /practices.json
def create
#practice = Practice.new(practice_params)
respond_to do |format|
if #practice.save
format.html { redirect_to #practice, notice: 'Practice was successfully created.' }
format.json { render json: #practice, status: :created, location: #practice }
else
format.html { render action: "new" }
format.json { render json: #practice.errors, status: :unprocessable_entity }
end
end
end
# PUT /practices/1
# PUT /practices/1.json
def update
#practice = Practice.find(practice_params)
respond_to do |format|
if #practice.update_attributes(practice_params)
format.html { redirect_to #practice, notice: 'Practice was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #practice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /practices/1
# DELETE /practices/1.json
def destroy
#practice = Practice.find(params[:id])
#practice.destroy
respond_to do |format|
format.html { redirect_to practices_url }
format.json { head :ok }
end
end
end
Model:
class Practice < ActiveRecord::Base
#has_many :providers, :patients, :employees, :operatories, :clinics, :patients,
has_many :imagecategories
validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name)
def initial_setup
Operatory.create(:name => "Default Operatory", :active => true, :practice => self)
####### Create Practice Basic Accounts:
Revenue.create(:name => "General Revenue", :practice => self)
end
end
Schema (relevant part of):
create_table "practices", force: true do |t|
t.string "name"
t.text "billing_address"
t.text "physical_address"
t.string "phone_number"
t.string "fax_number"
t.string "emergency_contact"
t.string "emergency_phone"
t.string "email_address"
t.boolean "active"
t.string "ABN"
t.datetime "created_at"
t.datetime "updated_at"
t.string "time_zone"
end
Any help getting this sorted out would be much appreciated, maybe I am tired and just missing something obvious. But I copied most of the code from other controllers that are working correctly andthink I have made all the necessary adjustments. Any help would be much appreciated
PS its not just limited to the :name parameter, when I remove that one it simply starts complaining about:billing_address
In update action, change
#practice = Practice.find(practice_params)
to
#practice = Practice.find(params[:id])

Resources