I'm trying to add Bootstrap Markdown to a Rails app, using a gem. I am also trying to use simple_form to format the layout.
https://github.com/dannytatom/rails-bootstrap-markdown
Bootstrap Markdown requires a data-provide attribute, as shown in the html code from the project's github site: http://toopay.github.io/bootstrap-markdown/
<textarea name="content" data-provide="markdown" rows="10"></textarea>
But when I try to do this in my simple_form, I get an error: wrong number of arguments (0 for 1..2)
<%= simple_form_for(#essay) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :class %>
<%= f.input :title %>
<%= f.input :essay, :input_html => {:rows => 5, :placeholder => "Enter some text.", :class => "span6", :data-provide => "markdown" }%>
<%= f.input :status %>
</div>
Ruby doesn't accept a '-' in a symbol... You need to provide your data-provide as a string... Everything else looks okay. e.g.
<%= f.input :essay, :input_html => {:rows => 5, :placeholder => "Enter some text.", :class => "span6", "data-provide" => "markdown" }%>
Or, optionally I think you could also use the Rails data hash like so:
<%= f.input :essay, :input_html => {:rows => 5, :placeholder => "Enter some text.", :class => "span6", :data => {:provide => "markdown"} }%>
The latter method might be better if you're providing a lot of data attributes, but for a single attr the first seems more readable.
Related
In Rails 3.2 - I sometimes use :input_html on forms.
For example:
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => {:checked => true} %>
If the user unchecked the box and submits the form and there are some validation errors, the check box gets checked again.
Is there a way to leave it unchecked?
Thanks for your help!
UDPATE1
I changed the code to this:
<% if params.has_key?(:assign_client) %>
<%= f.input :assign_client, :label => 'Charge Client?' %>
<% else %>
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => {:checked => true} %>
<% end %>
But, that didn't work.
The params hash will contain the assign_client key if the checkbox was checked by the user. So, you could do something like this:
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => { :checked => params.has_key?(:assign_client) } %>
I am using simple_form in rails and have a situation where I want the label to be the value from another field. In this case that field is not to be changed and so I don't want to be on the form.
To explain a bit better I have two lines that look like
<%= f.input :name, :label => false, :disabled => true, :input_html => { :class => 'input-small' } %>
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => false %>
What I'd like to do is have the first element to be the label of the second element. Now I could do this by having them inline, but I'd like them to be lined up with the other elements so that the labels and inputs are lined up.
so doing something like
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => f.name %>
or
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => {f.input :name, :label => false, :disabled => true} %>
Any thoughts on how to get around this?
Michael
'f.object' gets the object associated to that form and then you can get to the fields:
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]), :label => f.object.name %>
In the end I did indeed go for the inline option, which isn't ideal but did the job. However I had to do the following.
The main form set as a norm form
The block below that set as form-horizontal
then the subform partial defined as a none simple_form
<div class="control-group form-inline">
<div class="controls">
<%= f.text_field :name, :disabled => 'true', :size => 10 %>
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]) %>
</div>
</div>
If it was set as a simpleform the inline would not work as required. Again not perfect, and certainly not elegant, but worked
I have a simple form:
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :input_html => { :value => params['type'] } %>
<%= f.input :name, :label => "Organization" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
A user gets to this page through a url like http://www.website.com/org/signup?type=Company
I can use this format to enter a value into a field like name or email, but can't figure out how to pass the param to the drop down.
I've already tried a few things including changing :value to :selected or :option but nothing seems to work.
Alright, figured it out! Posting here for future use.
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :selected => params['type'] %>
The trick is to drop the :input_html part and just use
:selected = > params['type']
Hope that helps someone in the future!
I'm starting to use simple_form for a rails application, and while converting some of my forms, I came across one that has two models that it is working with, sort of an embedded form. Is this possible with simple_form?
<% simple_form_for :topic, :url => forum_topics_path do |t| %>
<%= t.input :name, :label => 'Topic' %></p>
<p>First Post:<br/></p>
Title: <%= text_field :post, :title %> <--- this is where i start having problems
Body: <%= text_area :post, :body %>
<%= t.submit 'Save' %>
Thanks
Use simple_fields_for :
<%= simple_form_for :topic, :url => forum_topics_path do |topic_builder| %>
<%= topic_builder.input :name, :label => 'Topic' %>
<%= topic_builder.simple_fields_for :post do |post_builder| %>
<p>First Post:</p>
<%= post_builder.input :title, :input_html => { :size => 30 } %>
<%= post_builder.input :body, :as => :text, :input_html => { :rows => 20, :cols => 50, :class => 'resizable' } %>
<% end %>
<%= topic_builder.submit 'Save' %>
<% end %>
Notes
Note the = symbol in <%= simple_form_for ... and <%= simple_fields_for (required in Rails 3.x)
Removed "Title:" and "Body:" text. Use the label generated for the inputs and style their location with CSS as needed.
Added example of using input_html
There's another approach that I'm using and it works great. Ryan Bates (RailsCasts) has created a gem to handle this.
See https://github.com/reu/simple_nested_form for the details.
Am supposed to be learning French at the moment, but rather than learning any vocab, I've been mucking around with a rails app that tests vocab - so it displays a word, and I have to type its translation.
Unfortunately, Firefox remembers everything I've already type there, so which diminishes its usefulness somewhat.
Is it possible, through the options for form_for or otherwise, to turn this normally useful behaviour off?
So it turns out it's pretty simple. Rather than
<%= f.text_field :fieldname %>
put
<%= f.text_field :fieldname, :autocomplete => :off %>
You can also turn off autocomplete at the form level by using the :autocomplete attribute in the :html collection, which will generate the HTML that Erv referenced. The syntax is
<% form_for :form_name, #form_name, :html => {:autocomplete => "off"} do |f|%>
...
<% end %>
Add autocomplete="off" as an attibute on your form tag:
<form action="..." method="..." autocomplete="off" >
</form>
I was using the "tag" variant of forms and found this to work:
<%= text_field_tag('favorite animal', nil, :options => {:autocomplete => 'off'}) %>
Below the tag variant for Rails 3 applications:
<%= text_field_tag :search, nil, :autocomplete => 'off' %>
This worked for me in Rails 4+
<%= f.text_field :name, autocomplete: :off %>
Nice and simple
Simple solution for me in Rails 4+
In the form i added:
:autocomplete => "off"
And in the field:
:autocomplete=>"none"
Example:
<%= form_for(#user.address_detail,
:url => {:action => :update_address},
:validate => true,
:method => :put,
:html => {:class => "form-horizontal",:autocomplete => "off"},
) do |f| %>
<div class="controls">
<%= f.text_field :address_line_1,:autocomplete=>"none" %>
</div>
If you use simple_form_for you can use autocomplete also bur only with input_html if you use it on a specific field.
For example :
<%= f.input :password, input_html: { autocomplete: "new-password" } %>