i still try in rails 3.2 to use bootstrap typeahead, it works fine, but how can i avoid users to type anything in text field.
I code looks like this
<%= text_field_tag(:text, "", :data => {:provide => "typeahead", :items=>"4", :source=>'["Swiss","German","English"]'}) %>
users may only type Swiss, German and English in the text field, but not for example Chinese.
How can i validate the value in text field?
Set tag attribute disabled="disabled"
Related
I generate my simple pubs _edit form with
= form_for #pub do |f|
- if #pub.errors.any?
-# fields
GETing /ldc/pubs/1/edit (ldc -> namespace)
the form tag gets a POST (?):
<form id="edit_ldc_pub_1" class="edit_ldc_pub" method="post" action="/ldc/pubs/1" accept-charset="UTF-8">
and everything is fine, but there is not route for that:
... others
DELETE /ldc/pubowners/:id(.:format) ldc/pubowners#destroy
ldc_pubs GET /ldc/pubs(.:format) ldc/pubs#index
POST /ldc/pubs(.:format) ldc/pubs#create
new_ldc_pub GET /ldc/pubs/new(.:format) ldc/pubs#new
edit_ldc_pub GET /ldc/pubs/:id/edit(.:format) ldc/pubs#edit
ldc_pub GET /ldc/pubs/:id(.:format) ldc/pubs#show
PUT /ldc/pubs/:id(.:format) ldc/pubs#update
DELETE /ldc/pubs/:id(.:format) ldc/pubs#destroy
shouldn't there be an error?
It is not a problem, because PUT and POST are somehow redundant including the presence of an :id
but can I rely on that? And why is there a POST rendered?
Last question first:
A "post" is rendered because you really only have GET and POST as options in the form field.
Title question:
What you're missing is that there's a hidden value, e.g.,
<input name="_method" type="hidden" value="put" />
This is rendered by the form_for helper. Rails uses the value of _method to find the correct route.
The HTTP method from the browser is a POST, but Rails uses _method if present.
I'm having problems with the select tag in my view.
First of all I'm using Rails 3.1.1 and Ruby 1.9.2 p290
The model is a Consumable which has a name, supplier, reorder number, type and associated printer models that it is compatible with.
The field that isn't validating correctly is type which is fed values from an array in the Consumable model and the validation should be against inclusion of one of the elements of the array.
In the model, I define the field's validation like so
TYPES = [ "REM", "REM-HICAP", "OEM", "OEM-HICAP" ]
validates :type, inclusion: TYPES
In the view (form partial for Consumable) I've created the select tag thusly
<div class="field">
<%= f.label "Type" %><br />
<%= f.select :type, Consumable::TYPES, prompt: 'Select a type...',
:selected => params[:type] %>
</div>
The drop-down box is creating well-formed HTML which looks like this
<div class="field">
<label for="consumable_Type">Type</label><br />
<select id="consumable_type" name="consumable[type]">
<option value="">Select a type...</option>
<option value="REM">REM</option>
<option value="REM-HICAP">REM-HICAP</option>
<option value="OEM">OEM</option>
<option value="OEM-HICAP">OEM-HICAP</option>
</select>
</div>
When I post the form this is the debug information
---
utf8: ?
_method: put
authenticity_token: nZZ9MastYswVCDrvAbgVLTUWqSRZLVrvRLmxOqPYk7I=
consumable:
description: Black Toner
supplier_id: '1'
reorder_no: '123456'
type: OEM
printer_model_ids:
- '1'
- '3'
commit: Update Consumable
action: update
controller: consumables
id: '1'
As is evident by the debug info, type is set correctly with a value from the select box (a string) and type in the model is a string. The form instead shows the validation error "Type is not included in the list" and if I remove the validation the database shows a nil value for the type field.
At what point is the validation applied to the form, does the controller trigger it by attempting to save the object?
I have used the same method of validating a drop-down box in the depot application in Agile Web Development with Rails 4th Edition with essentially the same code but a different name for the array and it works correctly.
I have just tested the same validation in the depot application and it works on this same workstation, so I would count out setup inconsistencies.
Thanks
I finally discovered my error: Type is protected in Rails and changing my variable name to Kind make it work correctly.
I have a textarea which is has Markdown support and I would like to show a placeholder to show what sort of formatting Markdown likes but putting <br /> in the placeholder shows <br /> in the text. I would prefer it to make a new line if this is possible.
I am using Rails 3, below is the code I am using:
<%= f.text_area :info, :placeholder => "if you want you can <br /> add a link by doing this: [text](http://link.com) it's pretty neat aye? or you could use bold by doing this: **bold text is cool**" %>
According to the specification, the placeholder attribute can't contain any line breaks or carriage returns.
As Logan says, though you can find some hacks in the similar question here:
Can you have multiline HTML5 placeholder text in a <textarea>?
Here are some more related hacks to multiline placeholders, I followed the example in the answer below to do my workaround:
Insert line break inside placeholder attribute of a textarea?
For rails, I thought this was useful if you want a gem to deal with placeholders:
jquery-placeholder-rails.
However, you'll need the forked version of jQuery-placeholder that supports newline: jQuery-Placeholder-Newlines
I have some simple erb code in one of my views in a rails project.
<%= comment.body %>
I'd like the html tags in the comment.body to be preserved as they have formatting information. I've verified that the text is saved in the database properly like
<b>hello</b>
However it turns out on the page to be <b>hello</b> not hello as I expect.
How could this be? I'm not using <%= h to escape the html code.
How do I make it not escaping? I'm using rails 3. Does this matter?
You can also use sanitize.
<%= sanitize(comment.body) %>
sanitize will leave html code but escape javascript.
Rails 3 now automatically escapes your output.
To unescape the text and use the actual tags, use raw(...):
<%= raw(comment.body) %>
However, be careful with this, as it will allow any tags, including scripts (potentially malicious). A safer option might be to have users use markdown-formatted text or something similar, rather than allowing raw HTML tags.
In a Rails 3 application I have a domain class where one attribute stores pure HTML content (it's a blog app, the domain class is Post).
In the ERB templates, I need to display the content of the attribute as it was formmated, with the HTML tags in place. But, Rails is escaping all HTML tags! How can I disable this behaviour for this class attribute?
Example:
somePost = Post.new
somePost.content = "<strong> Hi, i'm here! </strong>"
In the erb template:
<%= somePost.content %>
The HTML generated is escaped:
<strong> Hi, i'm here! </strong>
Try using raw(somePost.content). Alternatively, somePost.content.html_safe.
Use raw(string), as described in the release notes.
7.4.3 Other Changes
You no longer need to call h(string) to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call raw(string).
Basically, where you did
<%=h #model.attr %>
before you can now use
<%= #model.attr %>
and where you did that before you can now use
<%=raw #model.attr %>
Using a double equals means the result is not escaped...
<%== somePost.content %>
See this SO question about it - What does <%== %> do in rails erb?