フォームでは、クリックするとチェックボックスがtrueを返します。チェックボックスがチェックされているかどうか
フォームであなただけの指定:
コントローラーあなただけのフォームから返されたものを保存して
<%= f.check_box :terms_of_service %> Accept Terms of Services
check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
:私は、あなたの移行で
#you whitelist the parameter together with all the other params you have
def user_params
params.require(:user).permit(:terms_of_services)
end
#You create your user as usual
def create
@user = User.new(user_params)
end
より明示的にするためにterms_of_servicesのデフォルトオプションを追加します。
class AddTermsOfServicesToUser < ActiveRecord::Migration
def change
add_column :users, :terms_of_services, :boolean, default: false
end
end
EDIT
あなたはUserモデルに列を作成しない場合、あなたは、サーバー側のサービスの条件で動作する方法はありません。列を作成せずに必要な場合にのみ、クライアント側のterms_of_serviesで作業できます。
<input type="checkbox" id="cbox1" value="accepted" onchange='enableSubmitTag(this)'>
<label for="cbox1">Accept Terms of Services</label>
を無効(それはあなたがデータベースには列を持たないので、あなたが持っていないオブジェクトを必要とするため)
はRailsのCHECK_BOXヘルパーなしのチェックボックスを作成します。これは、あなたがそれを行うことができる方法でありますデフォルトで[送信]ボタン
<%= f.submit "Create", class: "btn btn-primary", id: "submit_button", disabled: true%>
[サービスの条件]チェックボックスをオンにすると、[送信]ボタンが再び有効になります。
function enableSubmitTag(element) {
if(element.checked){
document.getElementById('submit_button').disabled = false;
} else {
document.getElementById('submit_button').disabled = true;
};
};
テーブルにterms_of_servicesを格納すると、サーバー側でも確認できます。あなたはすべてのJavaScriptを使用することができます。あなただけのチェックボックスを変更する必要があります。
<%= f.check_box :terms_of_service, :onchange => 'enableSubmitTag(this)' %> Accept Terms of Services
レール用の回答をフォーマットできますか? –
答えを更新しました。私はまた、あなたのスキーマでterms_of_servicesのデフォルト値をfalseに設定します。そうすれば、ユーザーは明示的にtrueに変更する必要があります。 –
私はテーブルに列として置く場合、それを使用する方法を知っています。私はそれを試してみることが可能かどうかを確認したかったのです。 –