Ruby on Rails与模型的关联

时间:2014-07-14 21:03:58

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我是初学者Ruby on rails.Application有4个model.State,Province,District和City。

模型

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 %>

2 个答案:

答案 0 :(得分:0)

请运行以下生成器,将state_id字段添加到proviences表。

rails g migration add_state_id_to_proviences state_id:integer

然后运行迁移命令

rake db:migrate

答案 1 :(得分:0)

您收到 state_id is nil ,因为您尚未在permitted params列表中找到它。

province_params方法更改为

def province_params  
  params.require(:province).permit(:name,:state_id)  
end

随着你得到的错误

  

省#show中的NoMethodError

很可能是由于此行<%= @state.name %>,因为@state中未定义show method

当您尝试显示<%= @province.state.name %>的{​​{1}}时,应为state name