Rails 3 Select box posts a blank variable but the HTML is well-formed - ruby-on-rails-3

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.

Related

POST same as PUT for /pubs/:id or is there an implicite route in RAILS

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.

Bootstrap typeahead avoid other value

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"

options_from_collection_for_select Definition

I have just read the Rails API definition for option_from_collection_for_select.
Returns a string of option tags that have been compiled by iterating over the collection and assigning the result of a call to the value_method as the option value and the text_method as the option text
Now I am very new to rails so was wondering if someone could break this down into smaller chunks and explain what is happening, dumb it down if you will, the explanation is very high level (well for me at the moment)
Thank you
Using the example from the Ruby on Rails API, let's assume you have a Person model that has an id attribute and a name attribute.
Say you have a form that creates a new project and assigns it to a person. Maybe you want to have a drop down select for what person you're assigning this project to. You could use options_from_collection_for_select for something like this.
<%= f.label :person, "Assigned Person" %>
<%= f.select(:person, options_from_collection_for_select(#people, "id", "name") )
(f by the way would be referring to the #project variable for our example form here.)
What this would do is create an option in your select drop-down for each person contained in the instance variable #people. Each of the <option> tags would have the id of that person assigned to its value attribute, and the text for that option would be the person's name.
So if your #people variable contained [#<Person id: 1, name: "Brock Sampson">, #<Person id: 2, name: "Byron Orpheus">], you would get HTML output like this:
<label for="project_person">Assigned person"</label>
<select id="project_person" name="project[person]">
<option value="1">Brock Sampson</option>
<option value="2">Byron Orpheus</option>
</select>
Does that make more sense?

RoR: why arent some of my microposts columns showing?

Here is the relevant part of the micropost view. .
<li>
<span class="content"><%= micropost.content %></span>
<span class="product"><%= micropost.product %></span>
<span class="price"><%= micropost.price %></span>
<span class="location"><%= micropost.location %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
I recently added product, price, and location to the columns in the micropost table. I checked in the database and these aspects are being recorded/written but for some reason content is the only one that gets displayed on the app. Why are the other ones left out?
Here is the generated HTML. Im assuming that this is the relevant section. as you can see the html for product, price and location is never generated.
<li id="1">
<img alt="alex" class="gravatar" src="http://gravatar.com/avatar/a6759dcef8e5aad4e77509e5479b9823.png?s=50" />
<span class="user">
alex
</span>
<span class="content">first</span>
<span class="timestamp">
Posted less than a minute ago.
</span>
delete
</li>
I dont think its the difference between attr_accessor and attr_accessible because content is attr_accessor and it shows up. also, I read the difference and it doesnt seem to be relevant here. I believe attr_accessible is the right one. plus I tried changing it to attr_accessor and it didnt work. Any other ideas? please and thank you!
Does the micropost that you are trying to see (ID = 1) have data populated on those fields (:product, :price, :location) ?
Since the generated HTML doesn't even have the span tags for those fields, the issue seems to be that Rails isn't loading the newest version of your view file. Are you running in production or in development? Have you re-started the Rails server?
Also, you are correct that attr_accessible is correct rather than attr_accessor if those are database columns.
You might also check if you have any kind of caching enabled that would be displaying a cached copy of the generated HTML. For example, you could change the content value in the database and see if that is reflected in the generated HTML.

Disable HTML escaping in erb templates

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?