为导轨设置创建表单 [英] Creating form for rails-setting

查看:154
本文介绍了为导轨设置创建表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用中,我使用 gem rails-settings 来保存用户电子邮件通知设置我正尝试创建一个表单来更新用户的通知设置。它应该由一组复选框组成,用户可以选择他们想要接收的电子邮件通知。

In my Rails app, I'm using the gem rails-settings to save a user's email notification settings. I'm trying to create a form to update a user's notification settings. It should consist of a set of checkboxes where the user selects what they want to receive email notifications about.

在rails-setting README中,没有例子说明如何将其与表单集成。我发现的最接近的教程在问题中列出:

In the rails-setting README, there are no examples of how to integrate it with a form. The closest tutorial I found was listed in the issues:

https://github.com/ledermann/rails-settings/issues/46

但我不确定实际情况表单元素看起来像我的看法。例如,如果我想创建一个看起来像这样的输入:

But I'm not sure what the actual form element would look like in my view. For example, if I wanted to create an input that looked something like this:

<input name="settings[email][comments]" type="textbox" value="1">

如何使用Rails窗体助手来生成它?

推荐答案

这就是我创建表单的方式。希望这可以帮助别人。

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

edit.html.erb

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

这篇关于为导轨设置创建表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆