I have a Zurb Foundation simple form with one field (:description) being a text area. I want the text area to be 10 rows deep but it appears as 2 rows. Anyone know how to change this?
<div class="row">
<div class="large-4 large-centered columns">
<%= simple_form_for(#venue) do |f| %>
<fieldset>
<%= f.input :name %>
<%= f.input :url %>
<%= f.input :street %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :description, as: :text, :input_html => { :cols => 5, :rows => 10 } %>
<%= f.submit "Submit", class: "button small radius" %>
</fieldset>
<% end %>
</div>
</div>
Since you are using bootstrap, default properties of bootstrap applies to your input textarea. It causes the problem. I had the same problem but, with columns.
You can overcome this problem by two solutions:
1.) Create a class and specify the height and width to be auto. Use the class wherever required.
Create this class in your application.css or any css file.
.test
{
width: auto;
height: auto;
}
Now use this class in your ruby code.
<%= f.input :description, as: :text, :input_html => { class: 'test', :cols => 5, :rows => 10 } %>
Since you have explicitly specified the width and height to be auto, the bootstrap's textarea width, height will be overrided by the local class 'test' width and height.
Hence :cols => 5, :rows => 10 properties will be set.
2.) Modify the input textarea properties in your bootstrap css file.
Change the height or width of input textarea to auto to avoid confusions or set it to a standard pixel if you are going to use the same width and height everywhere.
the class assignment doesn't work , try this instead
=f.input :description, label: false, :as => :text, :input_html => { :rows => 7 , :style => 'width: 100%'}
Works gre8 for me.!
Related
Rails 3.2
Bootstrap 3
In my view (app/views/users/password_expired/show.html.slim), I have:
- content_for :page_header do
= t('users.passwords.create_password')
= form_for(resource, as: resource_name, url: [resource_name, :password_expired], html: {method: :post, class: "form-horizontal new_user"}) do |f|
.form-horizontal-column.wide
.form-group
= devise_error_messages!
.form-group style="width: 600px"
= f.label :current_password, t('users.passwords.current_password')
= f.password_field :current_password
.form-group style="width: 600px"
= f.label :password, t('users.passwords.new_pass')
= f.password_field :password
.form-group style="width: 600px"
= f.label :password_confirmation, t('users.passwords.confirm_new_password')
= f.password_field :password_confirmation
div= f.submit t('users.passwords.change_my_password'), :class => "btn btn-primary btn-lg"
.form-horizontal-column.shared-links
I was expecting to see some padding, in between the labels/input fields, and the left side of the form, but I don't have any. It seems all the other forms are working fine.
How can I added some padding, to the left of the form-group, without modifying the bootstrap style, as the latter is working everywhere else?
Along with form-group, you can add some-css-class:
.form-group.some-css-class
Add the some-css-class in the label, if needed:
f.label :password, t('users.passwords.new_pass'), class: "some-css-class"
and then define it's style
.some-css-class {
/* define your margin/padding style */
}
And check if it works according to your spec...
How do I pass rows and columns size parameters in following code
<div class="field">
<%= f.label :Personal %><br />
<%= f.text_area :Personal %>
</div>"
For the f.text_area you can re-size it by doing something like:
<%= f.text_area :personal, :id => "message", :cols => 10, :rows => 10 %>
Seen here text_area
See the following links that will be useful in the future:
Ruby on Rails
RDOC_Main
I can't find how to display a dynamic label in Rails, i've tried using the :value => show_name property but it didn't work, it only displays Show name. Here is the view code
<p>
<div class="control-group">
<%= f.label :show_name, :value => :show_name, :class => 'control-label' %>
<%= #this next line fails with undefined method `show_name' for #<ActionView::Helpers::FormBuiler>
#f.label f.send :show_name, :class => 'control-label'
%>
<div class="controls">
<%= f.text_field :variable_value, :class => 'text_field' %>
<%= f.hidden_field :variable_id, :class => 'text_field' %>
<%= f.hidden_field :show_name, :class => 'text_field' %>
</div>
</div>
<p>
and if needed here is the show_name definition inside my model.
def show_name
Variable.find_by_id(self.variable_id).name
end
Ok, so i end up finding a solution that is very DRY, thank to this post. And the only thing im going to do is explain a lit bit more what to do:
First we are going to asume the most complex case in which we have nested forms and so we are using fields_for inside a form_for method:
<!-- f represents the form from `form_for` -->
<%= f.fields_for :nested_model do |builder| %>
<p>
<div class="control-group">
<!-- here we are just calling a helper method to get things DRY -->
<%= builder.label return_value_of_symbol(builder,:show_name), :class => 'control-label' %>
<div class="controls">
<%= builder.text_field :variable_value, :class => 'text_field' %>
<%= builder.hidden_field :variable_id, :class => 'text_field' %>
</div>
</div>
</p>
<% end %>
Note that we included the builder object(as specified in the fields_for call) in the parameters of our helper.
In our helper we define the return_value_of_symbol function
def return_value_of_symbol(obj,sym)
# And here is the magic, we need to call the object method of fields_for
# to obtain the reference of the object we are building for, then call the
# send function so we send a message with the actual value of the symbol
# and so we return that message to our view.
obj.object.send(sym)
end
Use label_tag, put the show_name on a instance variable on your controller and use like this:
<%= label_tag #show_name, nil, :class => 'control-label' %>
EDIT:
On your application_helper.rb, create a helper method similar to this one:
def show_name(name)
content_tag(:label, name, :class => 'control-label')
end
Then you can use the show_name(name) on your views like this:
<%= show_name(#name) %>
Just remember to populate the #name variable.
I've just started working with the acts_as_taggable gem. Really liking it so far, but I am a bit unclear about how to use this gem with a form.
class Photo < ActiveRecord::Base
acts_as_taggable_on :tags
end
In my form for Photos I am trying to implement a series of checkboxes for the user to assign tags to their photo:
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
When viewing the form I get this error:
NoMethodError in Photos#edit
...line #19 raised:
undefined method `merge' for "landscape":String
Extracted source (around line #19):
18: <div class="float_tag">
19: <%= f.check_box :tag_list, "landscape" %>
Any thoughts as to how I should create my form?
I'm assuming your <form> looks something like this:
<%= form_for(#photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
<% end %>
You should change up your f.checkbox lines a bit:
<%= form_for(#photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, { :multiple => true }, 'landscape', nil %>
<%= f.check_box :tag_list, { :multiple => true }, 'people', nil %>
<% end %>
Which will post something like this when submitted (with only people selected, for example):
{ :post => { :tag_list => ['', 'people'] } }
For anyone trying to get this to work with Rails 4 and strong parameters, I also had to permit the the tag_list param as an array.
params.require(:clip).permit(
:name, :other_params, { tag_list: [] }
)
I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end