Skip to content

Instantly share code, notes, and snippets.

@mankind
Last active August 14, 2019 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mankind/ceed716cf69e326668ae0cb5b4f76f9a to your computer and use it in GitHub Desktop.
Save mankind/ceed716cf69e326668ae0cb5b4f76f9a to your computer and use it in GitHub Desktop.
rails in-place edit with gems
<% @account = account %>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default account-form">
<%= form_for(@account, url: update_single_admin_account_setting_path(@account), :html => { class: 'form control' }) do |f| %>
<div class="panel-body">
<% if @account.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@account.errors.count, "error") %> prohibited this account from being saved:</h2>
<ul>
<% @account.errors.messages.each do |key, messages| %>
<li><%= key %> &quot;<%= @account.errors.details[key].first[:value] %>&quot; <%= messages.join(' and ') %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.fields_for :settings do |ff| %>
<%= ff.label :email %><br>
<%= ff.text_field :contact_email, :value => @account.contact_email, class: 'form-control' %><br>
<% end %>
</div>
</div>
<div class="panel-footer">
<%= f.submit class: 'btn btn-primary pull-right' %>
</div>
<% end %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default account-form">
<%= form_for(@account, url: update_single_admin_account_setting_path(@account), :html => { class: 'form control' }) do |f| %>
<div class="panel-body">
<% if @account.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@account.errors.count, "error") %> prohibited this account from being saved:</h2>
<ul>
<% @account.errors.messages.each do |key, messages| %>
<li><%= key %> &quot;<%= @account.errors.details[key].first[:value] %>&quot; <%= messages.join(' and ') %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.fields_for :settings do |ff| %>
<%= ff.label :weekly_email_list %><br>
<%= ff.text_field :weekly_email_list, :value => current_account.settings["weekly_email_list"], name: "#{f.object_name}[settings][weekly_email_list][]", class: 'form-control' %><br>
<% end %>
</div>
</div>
<div class="panel-footer">
<%= f.submit class: 'btn btn-primary pull-right' %>
</div>
<% end %>
</div>
</div>
</div>
class Ubiquity::AccountSettingsController < AdminController
layout 'dashboard'
before_action :set_account
def edit
respond_to do |format|
format.js {render template: "ubiquity/account_settings/settings/#{params[:partial_name]}.js.erb"}
format.html
end
end
def update_single
submitted_hash = account_params.to_h['settings']
hash_key = submitted_hash.keys.first
if hash_key.present?
#update only the hash key without overriding other content in the original hash
@account.settings[hash_key] = submitted_hash[hash_key]
#removes nil keys in the hash
@account.settings.compact
@account.save
end
redirect_to admin_account_settings_path
end
private
def set_account
@account = current_account
end
def account_params
params.require(:account).permit(:settings => [:contact_email, :index_record_to_shared_search, weekly_email_list: [],
monthly_email_list: [], yearly_email_list: []])
end
end
$('#link-email').replaceWith("<%= escape_javascript render 'ubiquity/account_settings/settings/contact_email', account: @account %>")
<% provide :page_header do %>
<h1><span class="fa fa-user"></span> Account Settings</h1>
<% end %>
<table class="table table-striped">
<thead>
<tr>
<th>Account Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td id="link-email"><b>Contact email:</b> <%= @account && @account.settings && @account.settings["contact_email"] %></td> <br>
<td><%= link_to "contact_email", edit_admin_account_setting_path(id: @account, partial_name: 'contact_email'), remote: true, class: "btn btn-xs btn-default" %></td>
</tr>
<tr>
<td id="weekly_email_list"><b>Weekly email list:</b> <%= @account && @account.settings && @account.settings["weekly_email_list"].try(:to_sentence) %></td> <br>
<td><%= link_to "weekly_email_list", edit_admin_account_setting_path(id: @account, partial_name: 'weekly_email_list'), remote: true, class: "btn btn-xs btn-default" %></td>
</tr>
<tr>
<td><b>Index records to shared search:</b> <%= @account && @account.settings && @account.settings["index_record_to_shared_search"] %> </td> <br>
</tr>
<tr>
<td><%= link_to "Edit your account settings", edit_admin_account_setting_path(@account), class: "btn btn-xs btn-default" %></td>
</tr>
</tbody>
</table>
scope :module => "ubiquity" do
resources :account_settings, path: "/admin/account_settings", controller: 'account_settings', as: 'admin_account_settings' do
member do
patch :update_single #, as: :update_single
end
end
end
$('#weekly_email_list').replaceWith("<%= escape_javascript render 'ubiquity/account_settings/settings/weekly_email_list', account: @account %>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment