【问题标题】:Ruby on Rails Association with modelRuby on Rails 与模型的关联
【发布时间】:2014-07-14 21:03:57
【问题描述】:

我是 Ruby on rails 的初学者。应用程序有 4 个模型。州、省、区和市。

型号

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。我为所有模型创建 CRUD。我的问题是

当我创建一些状态时。然后我在浏览器中创建省份并分配给状态 errorstate_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 %>

【问题讨论】:

  • 一方面,似乎没有任何方法称为provincial_params。
  • def province_params params.require(:province).permit(:name) end
  • 你能告诉我们你在submitform时生成的server log吗?
  • 尝试将您的 province_params 更改为 params.require(:province).permit(:name,:state_id)

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


【解决方案1】:

您将获得 state_id is nil,因为您在 permitted params 的列表中没有它。

将您的 province_params 方法更改为

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

你得到的错误

省份中的NoMethodError#show

很可能是由于&lt;%= @state.name %&gt; 这一行,因为@state 未在您的show method 中定义。

当您尝试显示associated provincestate name 时,它应该是&lt;%= @province.state.name %&gt;

【讨论】:

  • @MarySmith 很高兴为您提供帮助 :)
  • 我有另一个问题,我会更新所有详细信息。然后我在城市show.html.erb,在这种情况下显示城市如何属于地区,省和州?
  • 显示城市父级详细信息区、省和州
  • @MarySmith 很难猜出你想要什么。更好的方法是问一个新问题,清楚地描述你想要什么。你肯定会得到帮助:)
【解决方案2】:

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

rails g migration add_state_id_to_proviences state_id:integer

然后运行迁移命令

rake db:migrate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多