나는 초급 Ruby on rails.Application이 4 model.State, Province, District and City 있습니다.Ruby on Rails Association with model
모델
app/models/state.rb
Class State < ActiveRecord::Base
has_many :provinces
end
app/models/province.rb
Class Province < ActiveRecord::Base
belongs_to :state
has_many :districts
end
app/models/district.rb
Class District < ActiveRecord::Base
belongs_to :province
has_many :cities
end
app/models/city.rb
Class City < ActiveRecord::Base
belongs_to :district
end
Schema.rb
ActiveRecord::Schema.define(version: 20140714165543) do
create_table "states", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "provinces", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "state_id"
end
add_index "provinces", ["state_id"], name: "index_provinces_on_state_id"
create_table "districts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "province_id"
end
add_index "districts", ["province_id"], name: "index_districts_on_province_id"
create_table "citys", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "district_id"
end
add_index "citys", ["district_id"], name: "index_citys_on_district_id"
end
내가 simple_form의 gem.I을 사용하고는 모든 모델에 대한 CRUD 만들기입니다. 제 질문은
입니다. iam이 어떤 상태를 만들 때. 그러면 지방을 만들고 상태를 오류 브라우저에 할당합니다. STATE_ID은 지방에서
class ProvincesController < ApplicationController
#GET /Provinces/new
def new
@province = Province.new
end
# GET /provinces/1/edit
def edit
end
# POST /provinces
# POST /provinces.json
def create
@province = Province.new(province_params)
respond_to do |format|
if @province.save
format.html { redirect_to @province, notice: 'Province was successfully created.' }
format.json { render :show, status: :created, location: @province }
else
format.html { render :new }
format.json { render json: @province.errors, status: :unprocessable_entity }
end
end
end
end
_form.html.erb 전무
<%= simple_form_for(@province) do |f| %>
<% if @province.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@province.errors.count, "error") %> prohibited this province from being saved:</h2>
<ul>
<% @province.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.association :state %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
province_params라는 메서드가있는 것처럼 보이지 않습니다. –
'def province_params' 'params.require (: province) .permit (: name)' 'end' –
'form'을 제출할 때 생성 된'서버 로그 '를 보여줄 수 있습니까? – Pavan