2016-09-08 9 views
0

にルビーをf.selectする配列を渡す:は私がf.selectフィールドのコードを持つビュー/カード/ _formでの私のRailsアプリケーションでレール

:私に出力を与える

<%= f.select :tag_speciality, options_for_select(@subdomain.tag_speciality), {}, {class: 'form-control'} %> 

私は持っている私のマイグレーションファイルで

<select class="form-control" name="card[tag_speciality]" id="card_tag_speciality"> 
    <option value="Professor de Yoga">Professor de Yoga</option> 
    <option value="Professora de Yoga">Professora de Yoga</option> 
    <option value="Estúdio de Yoga">Estúdio de Yoga</option> 
</select> 

add_column :cards, :tag_speciality, :string, array: true 

私はフォームに移動し、任意のオプションを選択して、例えば、「教授・ド・ヨガ」、および保存、私を得ます結果:

[] 

の代わり:

["Professor de Yoga"]

は、これは私のコントローラです:

def index 
 
    @cards = Card.all 
 
    end 
 

 
    def create 
 
    @card = @user.cards.new card_params 
 

 
    respond_to do |format| 
 
     if @card.save 
 
     format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully created.' } 
 
     format.json { render :show, status: :created, location: @card } 
 
     else 
 
     format.html { render :new } 
 
     format.json { render json: @card.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end 
 

 
    def update 
 
    respond_to do |format| 
 
     if @card.update(card_params) 
 
     format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully updated.' } 
 
     format.json { render :show, status: :ok, location: @card } 
 
     else 
 
     format.html { render :edit } 
 
     format.json { render json: @card.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end 
 
    
 
    # Never trust parameters from the scary internet, only allow the white list through. 
 
    def card_params 
 
    params.require(:card).permit(
 
     :name, 
 
     :phone, 
 
     :email, 
 
     :cover, 
 
     :is_published, 
 
     :subdomain_id, 
 
     :domain_id, 
 
     :profile_id, 
 
     :is_solidarity, 
 
     :tag_speciality, 
 
     :tag_location, 
 
     user_id: [] 
 
    ) 
 
    end 
 
end

は、私が行方不明です何かありますか? ありがとう

+2

コントローラコードを表示 –

+0

あなたのコントローラに許可されたパラメータでtag_locationを入れましたか? –

+0

はい、ちょうどコントローラを追加しました。tag_locationは既にコントローラの許可されたパラメータにあります。問題は配列に関するものであるように見えます:真のフィールド、単純な文字列フィールドの場合はnormalyを保存します。 .. –

答えて

1

Ditto @ EJ2015。

ちょうどあなたのcreate

@card.tag_speciality.push(card_params[:tag_speciality]) 
@card.save 

を追加します。

respond_to do |format| 
     if @card.tag_speciality.push(card_params[:tag_speciality]) && @card.save 
     format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully created.' } 
     format.json { render :show, status: :created, location: @card } 
     else 
     format.html { render :new } 
     format.json { render json: @card.errors, status: :unprocessable_entity } 
     end 
    end 
+0

偉大な、それは働いて作成:)しかし、私はどのように更新することができますか?私は試みました: '@ card.tag_speciality.push(card_params [:tag_speciality])&& @ card.update(params)'が動作しませんでした! –

+0

問題は、@ card.update(params)を実行したときに、paramsの文字列値を配列の列に直接割り当てていることです。 createメソッドと同じ方法で、属性に値を割り当ててからオブジェクトを保存することができます。 – EJ2015

+0

働いて、ありがとう! –

1

tag_specialityは配列列なので、コントローラーの配列メソッドを使用してデータを割り当てる必要があります。

@card.tag_speciality.push(card_params[:tag_speciality]) 
@card.save 

もちろん、他の属性の割り当ても変更する必要があります。ちなみに、サーバーログに何が間違っているかを見ることができます。この場合、データベースがコミットを拒否したことを確認する必要があります。

+0

のマイグレーションファイルは、お返事ありがとうございます。コントローラでは、あなたが解決したコードを使用できますか? "def card_params"に入っていますか? –

+0

これはあなたの作成アクションと更新アクションに含まれます。あなたは@raviが示唆したようにそれを行うことができます。 – EJ2015

関連する問題