Skip to content

Instantly share code, notes, and snippets.

@altherlex
Last active February 13, 2017 20:23
Show Gist options
  • Save altherlex/c8d6c2f3d89750c551be to your computer and use it in GitHub Desktop.
Save altherlex/c8d6c2f3d89750c551be to your computer and use it in GitHub Desktop.
Simple_form in show
#config/initializers/simple_form/description_component.rb
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups
module Description
# Name of the component method
def desc(wrapper_options = nil)
object.send(attribute_name) if @builder.instance_variable_get('@lookup_action') == :show
end
end
end
end
SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::Description)
<%= simple_form_for @universe, html:{ role:"form"} do |f| %>
<%= nav_menu do |m| %>
<%= link_to 'Edit', edit_universe_path(@universe), class:"btn btn-primary active", role:'button'%>
<%= link_to 'Back', universes_path, type:"button", class:"btn btn-default" %>
<%end%>
<p id="notice"><%= notice %></p>
<%= f.input :name, :wrapper => :bs %>
<%end%>
<!--
#out html
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<p class="form-control-static">First Query</p>
</div>
</div>
-->
#config/initializers/simple_form.rb
config.wrappers :bs, :tag => 'div', :class => 'form-group' do |b|
b.use :html5
b.use :placeholder
b.use :label_text, :tag=> 'label', :class => 'col-sm-2 control-label'
b.use :readonly
b.wrapper :tag => 'div', :class => 'col-sm-10' do |ba|
ba.use :readonly
ba.use :desc, :tag=>'p', :class => 'form-control-static'
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
@woto
Copy link

woto commented Oct 12, 2015

Why not simply

app/inputs/static_input.rb

class StaticInput < SimpleForm::Inputs::BlockInput
  def input(wrapper_options = nil)
    content_tag :p, object.send(attribute_name), class: "form-control-static"
  end
end

and then

<%= f.input :email, as: :static %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment