第三次更新:好的,我想现在我很接近了。我现在正在创建数据,并且正在数据库中设置参数。我将业务控制器强参数更改为:
def business_params
params.require(:business).permit(:name, :description, :address_attributes=>[:line1, :line2, :city, :state, :zip])
当我编辑现有业务时,出于某种原因,我创建了两个地址。一个是空的,一个是完整的参数。所以,如果有人有最后的推动力,我认为我非常接近:)。
地址模型
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
商业模式:
class Business < ActiveRecord::Base
#each business belongs to user, through user_id
belongs_to :owner
has_one :address, as: :addressable
accepts_nested_attributes_for :address
# each business has many customers and has many services
has_many :customers
has_many :services
validates :owner_id, presence:true
validates_presence_of :name
#validates_length_of :state, is: 2
end
业务负责人:
class BusinessesController < ApplicationController
before_action :get_owner
before_action :set_business, only: [:show, :edit, :update, :destroy]
#helper_method :sort_column, :sort_direction
def index
@businesses = @owner.businesses
end
def show
#@customer = @business.customers.order(sort_column + " " + sort_direction)
end
def new
@owner = Owner.find(params[:owner_id])
@business = @owner.businesses.build
@address = @business.build_address(params[:address])
end
def edit
@address = @business.build_address(params[:address])
end
def create
@business = @owner.businesses.new(business_params)
@address = @business.create_address(params[:address])
respond_to do |format|
if @business.save
format.html { redirect_to owner_businesses_url(@owner), notice: 'Business was successfully created.' }
format.json { render action: 'show', status: :created, location: @business }
else
format.html { render action: 'new' }
format.json { render json: @business.errors, status: :unprocessable_entity }
end
end
end
def update
@address = @business.create_address(params[:address])
respond_to do |format|
if @business.update(business_params)
format.html { redirect_to owner_businesses_url(@owner), notice: 'Business was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @business.errors, status: :unprocessable_entity }
end
end
end
def destroy
@business.destroy
respond_to do |format|
format.html { redirect_to owner_businesses_url(@owner) }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_business
@business = @owner.businesses.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def business_params
params.require(:business).permit(:name, :description, :address_attributes=>[:line1, :line2, :city, :state, :zip])
end
def get_owner
@owner = Owner.find(params[:owner_id])
end
#def sort_column
# Customer.column_names.include?(params[:sort]) ? params[:sort] : "first_name"
#end
#def sort_direction
# %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
#end
end
表单视图:
<%= form_for([@owner, @business]) do |f| %>
<% if @business.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@business.errors.count, "error") %> prohibited this business from being saved:</h2>
<ul>
<% @business.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<%= f.fields_for :address do |address| %>
<%= p address.object %>
<div class="field">
<%= address.label :line1 %><br>
<%= address.text_field :line1 %>
</div>
<div class="field">
<%= address.label :line2 %><br>
<%= address.text_field :line2 %>
</div>
<div class="field">
<%= address.label :city %><br>
<%= address.text_field :city %>
</div>
<div class="field">
<%= address.label :state %><br>
<%= address.text_field :state %>
</div>
<div class="field">
<%= address.label :zip %><br>
<%= address.number_field :zip %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
现在我正在创建两个地址。一个是空的,一个是带参数的。差不多好了 :)。