Simple non-model check box properties

March 6, 2009

 
No Gravatar

Need to pass a property that isn’t associated with a model? I did. It took me a few minutes, but the solution is pretty easy.

Instead of passing the instance variable your form is using, try this.

<% form_for [:admin, @post]} do |f| %>
  <%= f.error_messages %>
 
	<p>
		<%= f.label :title %>
		<%= f.text_field :title %>
	</p>
 
	<p>
		<%= label(:skip_queue, "Skip queue") %>	
		<%= check_box("overrides", "skip_queue") %>	
	</p>
 
	<p>
		<%= f.submit "Submit" %>
	</p>
<% end %>

You’d access the property of the check box like so:

# log
Processing Admin::PostsController#create (for 127.0.0.1 at 2009-03-06 03:03:01) [POST]
  Parameters: {"commit"=>"Update", "post"=>{"title"=>"Title", "overrides"=>{"skip_queue"=>"1"}}
 
# access the property
params[:overrides][:skip_queue]
blog comments powered by Disqus