2

私は Rails に非常に慣れていないため、AJAX、UJS、および Rails を一緒に扱うときに多くの混乱が生じています。私はrailscastを見て、いくつかのSOの回答を見て、freenodeで#rubyonrails IRCチャネルを試しました。悲しいかな、私はまだ立ち往生しています。

とにかく、ここに私の問題があります。

SO私は建物と財産の2つのモデルを持っています。建物に属しているプロパティと、建物には多くのプロパティがあります。

プロパティに building_id として外部キーを追加しました。

現在、私の建物モデルには Method: self.search(search) があり、正しいアドレス (例: 999 Decarie) を指定すると、データベース内の Building テーブルから building_id が正しく返されます。

def self.search(search)
    #search.instance_variables.map {|v| "#{v}: #{search.instance_variable_get(v)}\n"}.join
    if ((search.nil?) || (search == ""))
        nil
    else
        search = search.to_s
        d { search }
        split = search.split(' ', 2)
        stnum = split.first
        d { stnum }
        stname = split.last
        d { stname }
        Building.where("streetno = ?", stnum).where("streetname = ?", stname).pluck(:id).first
    end
end

プロパティの部分的な _form には、collection_select を使用して、ユーザーが任意の建物の住所 (例: 999 Decarie) を選択できるようにする form_for ループがあります (選択/オプションの HTML ドロップダウン リストとしてレンダリングされます)。

<div class="field" id="selection">
   <%= f.collection_select :buildinginfo, Building.all, :half_address, :half_address, {:label => "Building Info"}%>
</div>  

では、目立たないjavascript/ajaxを使用するにはどうすればよいですか

A. ユーザーがフォームでそれを選択するとすぐに、コレクション select の選択された値を取得し、それを上記の建物モデル メソッド (self.search(search)) に渡します。これにより、正しい建物 ID が返されます。

B. メソッドによって返された建物 ID をすぐに取得し、フォームの非表示フィールド (Properties モデルの building_id フィールドに対応) に格納します。(以下のコードでは、値 1 を建物 ID に置き換えたい)

 <div class="field" id="selection_id">
         <%= f.hidden_field :building_id, :value => 1 %>  
      </div>    

したがって、建物を削除すると、関連するすべてのプロパティも削除されるように関連付けを機能させることができます。

さらにコードが必要な場合はお知らせください。Rails 4 を使用しています。どうもありがとうございました。

4

3 に答える 3

0

このリッチについて説明していただきありがとうございます。

あなたのアプローチは理にかなっていますが、私の最終目標は単純に建物 (has_many) とプロパティ (belongs_to) の間の関連付けを作成することであり、建物を削除するときに、それに関連付けられているすべてのプロパティも削除する必要があるため、別のアプローチを使用しました。

そのため、プロパティを作成/更新するときは、それを Building.properties 配列に追加する必要があります。これにより、プロパティの building_id フィールドが自動的に更新されます。

properties_controller の最終的なコードは次のとおりです。

def create
  @property = Property.new(property_params)

  **b_id = Building.search(@property.buildinginfo)**
  **Building.find_by(id: b_id).properties << @property**

  respond_to do |format|
    if @property.save
      format.html { redirect_to @property, notice: 'Property was successfully created.' }
      format.json { render :show, status: :created, location: @property }
    else
      format.html { render :new }
      format.json { render json: @property.errors, status: :unprocessable_entity }
    end
  end
end

def update
respond_to do |format|
  if @property.update(property_params)

    **b_id = Building.search(@property.buildinginfo)**
    **Building.find_by(id: b_id).properties << @property**

    format.html { redirect_to @property, notice: 'Property was successfully updated.' }
    format.json { render :show, status: :ok, location: @property }
  else
    format.html { render :edit }
    format.json { render json: @property.errors, status: :unprocessable_entity }
  end
end
end

Building.search 関数 (建物のモデルに入ります):

    def self.search(search)

    if ((search.nil?) || (search == ""))
        nil
    else
        search = search.to_s
        split = search.split(' ', 2)
        stnum = split.first
        stname = split.last
        s = Building.where("streetno = ?", stnum).where("streetname = ?",  stname).pluck(:id).first
        s
    end
end
于 2014-06-07T05:42:30.167 に答える