2017-08-15 4 views
0

radio_buttonsのデフォルト値をsimple_formに設定します。radio_buttonsのデフォルト値をシンプルに設定する方法

<%= f.input :library_type, as: :radio_buttons, collection: Library.library_types.collect { |k,_| [k.capitalize, k]} , checked: 'custom’%>

class Library < ActiveRecord::Base 
    enum library_type: [:standard, :custom] 
    ... 
end 

2.2.3 :002 > Library.library_types.collect { |k,_| [k.capitalize, k]} 
=> [["Standard", "standard"], ["Custom", "custom"]] 

私はオプションchecked: ‘custom’を追加しました:これは私のコードです。新しいライブラリを作成するときには、customがデフォルトで選択されます。

ただし、ユーザーが既にlibrary_typeを選択した場合にエラーが発生します。ユーザーがライブラリを編集すると、ユーザーがstandardを選択した場合でも、customが選択されます。

誰でもこれを解決する方法を知っていますか?ありがとう。

答えて

1

私はこのロジックをコントローラに移しました。 newのアクションでは、library_typeフィールドをcustomに設定する必要があります。

class LibrariesController < ApplicationController 
    def new 
    @library = Library.new(library_type: 'custom') 
    render 'edit' 
    end 

    def create 
    ... 
    end 

    def edit 
    #find @library here 
    render 'edit' 
    end 

    def update 
    ... 
    end 
end 

よう 何かがそう、それは、新しいインスタンスのcustomlibrary_typeを設定しますと、すでに作成されたレコードのためにそれを上書きしません。

+0

ありがとう、それは私が欲しい答えです。 – Stephen

関連する問題