Creating form for rails-setting

不羁的心 提交于 2019-12-06 14:38:06

This is how I created the form. Hope this helps someone else.

edit.html.erb

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
   <h4>Email notifications</h4>
   <div>Receive email notifications when</div>
   <div class="notification_settings_container" style="margin-bottom: 30px;">
        <%= fields_for current_user.settings(:email) do |settings_fields| %>
            <div class="notification_checkbox">
               <%= settings_fields.check_box :comments %> someone comments on my project
             </div>
             <div class="notification_checkbox">
                <%= settings_fields.check_box :followed %> someone follows me
             </div>
             <div class="notification_checkbox">
                <%= settings_fields.check_box :featured %> my projects get featured
             </div>
             <div class="notification_checkbox">
                <%= settings_fields.check_box :remixed %> someone remixes my project
             </div>
             <div class="notification_checkbox">
                 <%= settings_fields.check_box :collaborator %> someone adds me as a collaborator on a project
             </div>
             <div class="notification_checkbox">
                  <%= settings_fields.check_box :favorited %> someone favorites my project
             </div>
             <div class="notification_checkbox">
                  <%= settings_fields.check_box :collectify %> my project gets added to a collection
             </div>
         <% end %>
   </div>

   <%= f.submit "Update", :class=>"btn btn-small btn-info submitButton" %>
<% end %>

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def update
    if params[:rails_settings_setting_object]
            params[:rails_settings_setting_object].each do |key, value|
                Rails.logger.debug('setting email setting ' + key + 'to ' + value=="1")
                current_user.settings(:email).update_attributes! key.to_sym => value=="1"
            end
            redirect_to :back, notice: "Update email preferences!"
     end
   end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!