Padrino Admin Screen - haml

I am working on the Padrino admin screen and would like to show a dropdown list using haml in the edit view. In the List view I would like to show the country name rather than the id number it shows is this possible? I am a very newbie to haml.
I am using countries and states
_form.haml
<code>
- error = #province.errors.include?(:countries_id)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :countries_id, :class => 'control-label'
.controls
=f.select(:countries_id, :collection => #cntrylist, :fields => [:country_name, :id], :selected => :id, :include_blank => false , :class => :dropdown, :prompt => "Select Province/State")
%span.help-inline=error ? f.error_message_on(:countries_id, :class => 'dropdown-error') : pat(:example)
- error = #province.errors.include?(:province_name)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :province_name, :class => 'control-label'
.controls
=f.text_field :province_name, :class => 'form-control input-large input-with-feedback'
%span.help-inline=error ? f.error_message_on(:province_name, :class => 'text-error') : pat(:example)
- error = #province.errors.include?(:province_code)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :province_code, :class => 'control-label'
.controls
=f.text_field :province_code, :class => 'form-control input-large input-with-feedback'
%span.help-inline=error ? f.error_message_on(:province_code, :class => 'text-error') : pat(:example)
.form-actions
=f.submit pat(:save), :class => 'btn btn-primary'
=f.submit pat(:save_and_continue), :class => 'btn btn-info', :name => 'save_and_continue'
=link_to pat(:cancel), url(:provinces, :index), :class => 'btn btn-default'
</code>
This works as expected for new but for edit I would prefer to display the dropdown rather than id which it shows
index.haml
%tbody
-#provinces.each do |province|
%tr.list-row
%td.list-column.list-selectable
=check_box_tag 'province_ids[]', :value => province.id, :class => 'list-selectable-checkbox'
%td.list-column=province.id
%td.list-column=province.countries_id
...
In the td.list column where it shows the countries_id I would like to have this as the country_name.
Thanks

Related

simple_form custom layout for radio buttons

how the heck do you make a custom layout for radio buttons insimple_form?
this code generates a vertical list of radio buttons:
%fieldset
= f.input :my_collection, label: false, collection: MY_COLLECTION, as: :radio_buttons
but what i want, is for the radio buttons to be in separate columns. so i've output the literal radio buttons like:
.control-group.radio_buttons.required.my_collection
.controls
.row-fluid
.span3
%label.radio
= f.radio_button :my_collection, 'Foo', class: "radio-buttons required"
Foo
%label.radio
= f.radio_button :my_collection, 'Bar', class: "radio-buttons required"
Bar
.span3
%label.radio
= f.radio_button :my_collection, 'Biz', class: "radio-buttons required"
Biz
%label.radio
= f.radio_button :my_collection, 'Baz', class: "radio-buttons required"
Baz
seems to render OK, but the validations stop working. any ideas?
Check the custom wrappers docs. There are some examples with Boostrap or others frameworks.
For instance, you can add this in an simple_form initializer to create a custom checkbox wrapper:
SimpleForm.setup do |config|
config.wrappers :inline_checkbox, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.use :label_input, :wrap_with => { :class => 'checkbox inline' }
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
end
then use it like any other input but specifying the custom wrapper (Slim code):
= f.input :remember_me, wrapper: :inline_checkbox

Rails 3 Formtastic form not rendering

Here is my form view code:
<div class="row">
<%= semantic_form_for #new_athlete_sport, :remote => true, :html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
<%= f.label "Sport" %>
<%= f.select :sport_id, Sport.all.collect { |sp| [sp.name, sp.id] }, {}, { class: "chosen", id: "" } %>
<br />
<%= f.submit %>
<% end %>
</div>
For some reason the <form> tag isn't showing up in the DOM, but every other fields show up..
Seems the syntax in form_for has some problem.
Try remove "id" and move "data-type" from this
<%= semantic_form_for #new_athlete_sport, :remote => true,
:html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
to this
<%= semantic_form_for #new_athlete_sport, :remote => true, :data=> {:type=> 'json'}, :html => { :class => "new_sport"} do |f| %>

Activeadmin and Formtastic: form not responding to :size

I am trying to format a form and the text fields respond to some methods, and not others.
I can do things like:
f.input :name, :input_html => { :maxlength => 10 }
f.input :name, :input_html => { :disabled => true }
But if I try to do any of the following, they do not work:
f.input :name, :input_html => { :size => 10 }
f.input :name, :input_html => { :class => 'autogrow' }
f.input :name, :input_html => { :rows => 10, :cols => 10 }
When I try using :size, for instance, the generated html shows that size=10, but is not reflected in the actual form.
These were more or less pulled right from the Formtastic documentation on Github, which the Activeadmin documentation refers to.
I am not sure if your question is solved or not.
However according to Formastic Official WIKI, your code should work:
Customize HTML attributes for any input using the :input_html option.
Typically this is used to disable the input, change the size of a text
field, change the rows in a textarea, or even to add a special class
to an input to attach special behavior like autogrow textareas:
<%= semantic_form_for #post do |f| %>
<%= f.inputs do %>
<%= f.input :title, :input_html => { :size => 10 } %>
<%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
<%= f.input :created_at, :input_html => { :disabled => true } %>
<%= f.input :updated_at, :input_html => { :readonly => true } %>
<% end %>
<%= f.actions %>
<% end %>
https://github.com/justinfrench/formtastic
if your code doesn't work , please check out the error logs, or put more debug info to your erb file, to see if you r rails is running under production mode.
i had the same problem. i wanted a nested form for edit with custom text field size.this worked for me.
form do |f|
f.inputs "Header" do
cf.input :name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
f.actions
end
so basically u have to create your own class or just work with the :style.
For nested form u can use this code
form do |f|
f.inputs "Header" do
f.has_many :name,:allow_destroy => true,:new_record => true do |cf|
cf.input :first_name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
end
f.actions
end

form_tag url_for with :method "delete", id and :remote => true

I have the following form_tag working:
<%= form_tag url_for(:controller => "profiles", :action => "remove_academic", :method => :delete), :id => "remove_major_goal", :remote => true do %>
However, the HTML produced shows that :method => "delete" isn't working. So I found a few answers here on form_tag and tried this:
<%= form_tag url_for({ :controller => "profiles", :action => "remove_academic", :method => "delete" }, { :id => "remove_major_goal", :remote => true }) do %>
However that kicks back an error. What am I doing wrong?
DELETE is not a valid value of the method attribute for a HTML form element. You would probably be better inserting a <input type="hidden" name="method" value="delete" /> inside the form (or use a helper method to do so).
Update:
Try one of these:
form_for url_for(:controller => "", :action => ""), :method => "delete", …
form_for { :controller => "", :action => "" }, { :method => "delete", … }
The second set of braces in the second form maybe unnecessary. Likewise, they might be needed in the first form.

Rails 3 Switch menu reload page when change data

The user can edit his account and has a menu to choose his community :
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= f.collection_select :community_id, Community.find(:all,
:include => :memberships,
:conditions => ['memberships.user_id = ? and memberships.role > ?', #user.id, '0' ]),
:id,
:name,
:style => "width: 200px;" %>
<% end %>
I would like to put this menu on the show page and give him the possibility to switch directly.
With rails 2, I used observe_field or :onchange => 'this.form.onsubmit()' and I don't know java…
<%= form_for #user, :remote => true do |f| %>
<%= f.collection_select :community_id,
Community.where('memberships.user_id = ? and memberships.role > ?', #user.id, '0').includes(:memberships), :id, :name,{},
:onchange => "this.form.submit();" %>
<% end %>
Changed 'this.form.onsubmit() to 'this.form.submit()