undefined local variable or method `stop' for #<#<Class:0xb085c0a4>:0xb16e8c6c> - ruby-on-rails

I know this is a very common issue, but I can't resolve this with any of the other topics.
this is the issue:
undefined local variable or method `stop' for #<#:0xb16e8c6c>
app/views/stops/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>City name:</strong>
<%= #stop.city %>
</p>
<p>
<strong>Arrival time:</strong>
<%= #stop.Arrival_time %>
</p>
<p>
<strong>Route:</strong>
<%= #stop.Route_id %>
</p>
<%= link_to 'Edit', edit_stop_path(#stop) %> |
<%= link_to 'Back', stops_path %>
db/schema.rb
create_table "stops", force: :cascade do |t|
t.time "Arrival_time"
t.integer "Route_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "city"
end
app/controllers/stops_controller
class StopsController < ApplicationController
before_action :set_stop, only: [:show, :edit, :update, :destroy]
# GET /stops
# GET /stops.json
def index
#stops = Stop.all
end
# GET /stops/1
# GET /stops/1.json
def show
end
# GET /stops/new
def new
#stop = Stop.new
end
# GET /stops/1/edit
def edit
end
# POST /stops
# POST /stops.json
def create
#stop = Stop.new(stop_params)
respond_to do |format|
if #stop.save
format.html { redirect_to #stop, notice: 'Stop was successfully created.' }
format.json { render :show, status: :created, location: #stop }
else
format.html { render :new }
format.json { render json: #stop.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /stops/1
# PATCH/PUT /stops/1.json
def update
respond_to do |format|
if #stop.update(stop_params)
format.html { redirect_to #stop, notice: 'Stop was successfully updated.' }
format.json { render :show, status: :ok, location: #stop }
else
format.html { render :edit }
format.json { render json: #stop.errors, status: :unprocessable_entity }
end
end
end
# DELETE /stops/1
# DELETE /stops/1.json
def destroy
#stop.destroy
respond_to do |format|
format.html { redirect_to stops_url, notice: 'Stop was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_stop
#stop = Stop.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def stop_params
params.require(:stop).permit(:city, :Arrival_time, :Route_id)
end
end
I don't see where the problem is.. can you help me? thank you

You're getting this error because #stop isn't defined on the show page. You fix this by defining it in the show method in your Stop Controller.
I'm assuming the show page will pass a parameter for the shop id. If so you can define it like this:
def show
#stop = Stop.find(params[:id])
end

Related

Uploading audio files with carrierwave

I am having trouble uploading audio with the carrierwave gem, when i post the the audio file all that displays is the audio url link. First time using the gem and not sure if i am using carrierwave correctly. Just trying to display the audio file on the show page.
class AudioUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
storage :file
include CarrierWave::Audio
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #music.title %>
<%= #music.audio.url() %>
</p>
<%= link_to 'Edit', edit_music_path(#music) %> |
<%= link_to 'Back', musics_path %>
music.rb
class Music < ApplicationRecord
mount_uploader :audio, AudioUploader
end
schema
ActiveRecord::Schema.define(version: 20170606155943) do
create_table "musics", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "audio"
end
end
music controller
class MusicsController < ApplicationController
before_action :set_music, only: [:show, :edit, :update, :destroy]
# GET /musics
# GET /musics.json
def index
#musics = Music.all
end
# GET /musics/1
# GET /musics/1.json
def show
end
# GET /musics/new
def new
#music = Music.new
end
# GET /musics/1/edit
def edit
end
# POST /musics
# POST /musics.json
def create
#music = Music.new(music_params)
respond_to do |format|
if #music.save
format.html { redirect_to #music, notice: 'Music was successfully created.' }
format.json { render :show, status: :created, location: #music }
else
format.html { render :new }
format.json { render json: #music.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /musics/1
# PATCH/PUT /musics/1.json
def update
respond_to do |format|
if #music.update(music_params)
format.html { redirect_to #music, notice: 'Music was successfully updated.' }
format.json { render :show, status: :ok, location: #music }
else
format.html { render :edit }
format.json { render json: #music.errors, status: :unprocessable_entity }
end
end
end
# DELETE /musics/1
# DELETE /musics/1.json
def destroy
#music.destroy
respond_to do |format|
format.html { redirect_to musics_url, notice: 'Music was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_music
#music = Music.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def music_params
params.require(:music).permit(:title, :audio)
end
end

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 carrierwave multiple file upload error

I've been experiencing some issues with implementing multiple file uploads i've tried a few ways to get it to work namely the answers here Rails 4 multiple image or file upload using carrierwave. I couldn't get the first answer to work however i was able to get the second answer, and the one most similar to the carrierwave docs to add a entry to the database which looked like this;
images: [{"tempfile"=>[], "original_filename"=>"Cinque_Deck-and-Jacuzzi.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"location[images][]\"; filename=\"Cinque_Deck-and-Jacuzzi.jpg\"\r\nContent-Type: image/jpeg\r\n"}, {"tempfile"=>[], "original_filename"=>"cooking.jpeg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"location[images][]\"; filename=\"cooking.jpeg\"\r\nContent-Type: image/jpeg\r\n"}, {"tempfile"=>[], "original_filename"=>"hanging-rock.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"location[images][]\"; filename=\"hanging-rock.jpg\"\r\nContent-Type: image/jpeg\r\n"}]>
However when i try to display it, i get "NoMethodError in Locations#show", "undefined method `url' for #"
Can someone please tell me what i'm doing wrong? I've been working on this for days now and not getting anywhere.
The rest of my code is
show.html.erb
<%= image_tag #location.images[0].url, class: "display-location animated bounce" %>
<div class = "row hidden-sm-down">
<div class = "col-sm-4 hidden-sm-down">
<a href = "#" class = "thumbnail">
<%= image_tag #location.images[1].url %>
</a>
</div>
<div class = "col-sm-4">
<a href = "#" class = "thumbnail">
<%= image_tag #location.images[2].url %>
</a>
</div>
<div class = "col-sm-4">
<a href = "#" class = "thumbnail">
<%= image_tag #location.images[3].url %>
</a>
</div>
</div>
schema.rb
ActiveRecord::Schema.define(version: 20161210123055) do
enable_extension "plpgsql"
create_table "locations", force: :cascade do |t|
t.string "name"
t.string "address"
t.string "website"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.text "description"
t.string "price"
t.json "images"
end
locations controller
class LocationsController < ApplicationController
....
# GET /locations
# GET /locations.json
def index
#locations = Location.all
#locations = #locations.paginate(:page => 1, :per_page => 2)
end
# GET /locations/1
# GET /locations/1.json
def show
#random_location = Location.where.not(id: #location).order("RANDOM()").first(3)
#reviews = Review.where(location_id: #location.id).order("created_at DESC")
if #reviews.blank?
#avg_rating = 0
else
#avg_rating = #reviews.average(:rating).round(2)
end
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 :show, status: :created, location: #location }
else
format.html { render :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 { render :show, status: :ok, location: #location }
else
format.html { render :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, notice: 'Location was successfully destroyed.' }
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(:location).permit(:name, :price, :address, :website, :description, {images: []})
end
def check_user
unless current_user.admin?
redirect_to root_url, alert: "Sorry currently only admins have that functionality"
end
end
end
Thanks,
EDIT: Also noticed this ERROR bad URI/images/%7B%22tempfile%22=%3E[],%20%22original_filename%22=%3E%22Cinque_Deck-and-Jacuzzi.jpg%22,%20%22content_type%22=%3E%22image/jpeg%22,%20%22headers%22=%3E%22Content-Disposition:%20form-data;%20name=/%22location[images][]/%22;%20filename=/%22Cinque_Deck-and-Jacuzzi.jpg/%22/r/nContent-Type:%20image/jpeg/r/n%22%7D'.` in the terminal running the server
okay ended up having a extra s on images in the model file where the uploader is mounted... embarrassing but parsing data correctly now in development

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.

Address field doesn't show in new rails form

I'm building a program to keep a client database for my first Rails app. I've followed through several different tutorials and successfully built out the demo apps, but now I am trying to create my own. In an effort to keep the database organized and efficient at scale, I setup a relational database with foreign keys.
My new client form should be displaying three fields - First Name, Address ID and Street, but it is only showing the first two. I've run across a couple things suggesting using .build to initialize my street field, but adding #client.address.build to def new in my clients_controller.rb didn't help. Any suggestions as to what I'm doing wrong?
Schema:
ActiveRecord::Schema.define(version: 20141120045947) do
create_table "addresses", force: true do |t|
t.string "number"
t.datetime "created_at"
t.datetime "updated_at"
t.string "street"
end
create_table "clients", force: true do |t|
t.string "first_name"
t.integer "address_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "street_id"
end
add_index "clients", ["address_id"], name: "index_clients_on_address_id", using: :btree
end
Form partial:
<%= form_for(#client) do |f| %>
<% if #client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#client.errors.count, "error") %> prohibited this client from being saved:</h2>
<ul>
<% #client.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :address_id, "Address ID"%><br>
<%= f.text_field :address_id %>
</div>
<%= f.fields_for :address do |address| %>
<div class="field">
<%= address.label :street %><br>
<%= address.text_field :street %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Clients controller:
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
# GET /clients
# GET /clients.json
def index
#clients = Client.all
end
# GET /clients/1
# GET /clients/1.json
def show
#address = Address.find(params[:id])
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
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(:first_name, :address_id, address_attributes: [:number,:street])
end
end
Addresses controller:
class AddressesController < ApplicationController
before_action :set_address, only: [:show, :edit, :update, :destroy]
# GET /addresses
# GET /addresses.json
def index
#addresses = Address.all
end
# GET /addresses/1
# GET /addresses/1.json
def show
end
# GET /addresses/new
def new
#address = Address.new
end
# GET /addresses/1/edit
def edit
end
# POST /addresses
# POST /addresses.json
def create
#address = Address.new(address_params)
respond_to do |format|
if #address.save
format.html { redirect_to #address, notice: 'Address was successfully created.' }
format.json { render :show, status: :created, location: #address }
else
format.html { render :new }
format.json { render json: #address.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /addresses/1
# PATCH/PUT /addresses/1.json
def update
respond_to do |format|
if #address.update(address_params)
format.html { redirect_to #address, notice: 'Address was successfully updated.' }
format.json { render :show, status: :ok, location: #address }
else
format.html { render :edit }
format.json { render json: #address.errors, status: :unprocessable_entity }
end
end
end
# DELETE /addresses/1
# DELETE /addresses/1.json
def destroy
#address.destroy
respond_to do |format|
format.html { redirect_to addresses_url, notice: 'Address was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_address
#address = Address.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def address_params
params.require(:address).permit(:number, :street)
end
end
Client model:
class Client < ActiveRecord::Base
belongs_to :address
accepts_nested_attributes_for :address
end
Address model:
class Address < ActiveRecord::Base
has_many :clients
end

Resources