Rails 3 f.select not showing current value - ruby-on-rails-3

I have the following:
<%= f.select :phase_names, options_for_select(["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]), :class => 'inputboxes' %>
It is storing it in the database, but in the edit view it does not show the stored value. How do I show it?

This:
options_for_select(your_array_list, your_selected_value)

Related

Ruby on Rails default option value

I am using an array to store my values for a f.select dropdown in my form
<%= f.label :country_of_origin %>
<%= f.select :country_of_origin, [['Wales'],['Scotland'],['England']] %>
Rather than the default value being Wales I would like it to say "Please Select" (obviously don’t want to be able to post the value "please select). Can anyone point me in the right direction please?
<%= f.select("model_name", "model_id", [['Wales'],['Scotland'],['England']], {:include_blank => 'Please Select..'}) %>

How to access one model within another model in Rails..?

I have one data model 'object' with fields->object_id, object_name.
That is: http://localhost:3000/objects/
I have created another model 'front_pages' (not created any migration in this, instead I have created some pages like 'search.html.erb'(by hand) and the associated controllers).
That is: http://localhost:3000/front_pages/
My question is: How to access/search the items stored in the 'object' database within the 'search.html.erb'.
"These two are in the same rails project folder"
-> How to display the search results into an HTML.erb file?
views/static_pages/show.html.erb
<% #npsobject.each do |npsobjects| %>
Nps:
Nps type:
Nps name:
|
Static_page Controller
class StaticPagesController < ApplicationController
def show
#npsobject=Npsobject.find(:all, :conditions => ['nps_name LIKE ?', "%#{params[ :search]}%"]);
end
views/static_pages/new.html.erb
<%= form_tag( { :action =>"show"}, { :method => "get"}) do %> # The action path is ok??
<%= text_field_tag :search, params[:search], :class => 'inputBox' %>
"button") %>
Please verify the above codes and guide me through, as Im new to RoR..:)
You need to move your
#npsobject = Npsobject.find
into show action
and then each it into your views/static_pages/show.html.erb
<% #npsobject.each do |nps| %>
<%= nps.nps_name %>
<% end %>

Why is my nested text_area helper adding html tags?

I have a text_area in a partial in a complex form that is called like so
<%= f.fields_for :notes do |notes_form| %>
<%= render :partial => 'note', :locals => {:f => notes_form, :operation => f, :count => operation.notes.count} %>
<% end %>
<p><%= add_child_link "Add note", :operation_notes %></p>
and the partial looks like this
<% count ||= 2 %>
<div class='fields'>
<%= f.text_area :note_text, :rows => "4", :class => "notes" %>
<%= remove_child_link "x", f, count %>
</div>
There can be many notes on the form hence the add and remove child links.
The issue I'm having is that if I add a note with the text 'abcd', when I bring up the edit form I get '<p>abcd</p>'. If there are line breaks in the note it adds <br /> tags. The text_area form helper seems to be using the simple_format helper but I have no idea why. Can anyone help as this is very undesirable behaviour?
Ah solved,
Earlier on the same page I was displaying the note and using simple_format to format it with
<%= simple_format note.note_text %>
It seems that simple_format is somewhat destructive as after this, a call to note.note_text always returns the formatted text. If I change the above to
<%= simple_format note.note_text.dup %>
then the note_text attribute is not altered and I get the appropriate results.
I will have to look more closely at simple_format but this really strikes me as undesirable behaviour.
EDIT
It looks like this has been corrected in Rails 3.1
I would suspect that you have something in your Note model that is processing the text. Check for callbacks in this model.

set my state with collection_select with pluginaweek/state_machine

How do I create a collecton_select drop down to set the state of a resource in a form?
right now I have
<%= f.collection_select :state, Ticket.state.all, :event, :state_name_humanize, :prompt => true %>
(could pluginaweek make a 'getting started with state_machine' tutorial, I am really hot on using it, but I am a nube . . .)
From the example a collection select should look like this: <%= f.collection_select :access_state_event, #user.access_state_transitions, :event, :human_event, :include_blank => "don't change" %>
Would also like to see a simple example/tutorial for active record

Difference between two f.select option

Hopefully this is just a quicky....
I have a form to edit a product and each product belongs to a category.
In the form_for(#product) I can populate a select box for the categories in a couple of ways:
<%= f.select :category_id, Category.find(:all).collect{|c| [c.category, c.id]}, :prompt => "Pick a Category" %>
or:
<%= f.select :category_id, options_from_collection_for_select(Category.find(:all), :id, :category) %>
The first option remembers the category when editing the product, the second option doesn't.
Can anybody enlighten me as to why? Is there a way to use the options_from_collection_for_select in this scenario and have it remember the category upon editing?
Cheers,
Adam
The Codeglot's answer should have been:
<%= f.collection_select :category_id, Category.all, :id , :name %>
(See Rails: undefined method `map' for Ingredient for explanation)
<%= f.collection_select :category_id, Category, :id , :name %>
make sure you change :name to the field that you want displayed. It's probably :name or :title
Try this:
<%= f.select :category_id, options_from_collection_for_select(Category.find(:all), :id, :category, params[:category_id].to_i) %>