Let's say you provide a select menu for setting which category a given product belongs to, but you also want the option of creating a new category by typing the name in a text field. See a great way to do that in this episode.
<!-- views/products/_form.rhtml -->
<p>
<label for="product_category_id">Category:</label><br />
<%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => "Select a Category" %>
or create one:
<%= f.text_field :new_category_name %>
</p>
# models/product.rb
belongs_to :category
attr_accessor :new_category_name
before_save :create_category_from_name
def create_category_from_name
create_category(:name => new_category_name) unless new_category_name.blank?
end
分享到:
相关推荐
Create ML: Learn how to create your own model using Apple’s Create ML Tool. Turi Create and Keras: Learn how to tune parameters to improve your machine learning model using more advanced tools. Image...
Create ML: Learn how to create your own model using Apple’s Create ML Tool. Turi Create and Keras: Learn how to tune parameters to improve your machine learning model using more advanced tools. Image...
Create ML: Learn how to create your own model using Apple’s Create ML Tool. Turi Create and Keras: Learn how to tune parameters to improve your machine learning model using more advanced tools. Image...
Step Relational Database DesignTM uses three case studies and starts with a statement of the problem by the client and then goes through the six steps necessary to create a reliable and accurate data ...
- **Developing Custom Field Types:** Steps to create custom field types. - **Field Rendering Control:** How to control the display of fields using templates and controls. - **Supporting Mobile Devices...
// create the datastore service class $datastore = new Google_Service_Datastore($client); // build the query - this maps directly to the JSON $query = new Google_Service_Datastore_Query([ 'kind' => ...
20. Marmot Dam: Orthophotographyand Colorized Slope Model 50 21. LiDAR Point Density versus Interpolation 53 LIST OF TABLES T~k p~ 1. Reported Accuracies of 2006 and 2007 LiDAR 24 2. Results of LiDAR ...
To use this system, select your supported hp probook/elitebook model from the dropdown list. The Serial Number field and UUID Filed will populate with data and will need to be changed to that of your ...
This book is uniquely designed for developers who are looking to transition their skills into the .NET development field The standalone chapter structure leaves you free to explore ASP.NET MVC to ...
.This model makes it easy to extend the application’s data model to meet all users’ individual needs, and restoring a user’s data from backups in a failure is a relatively simple thing[9].But ...
- **Custom Template Tags:** Developers can create their own template tags to perform complex operations or repetitive tasks within templates. - **Tag Registration:** Custom tags must be registered in ...
- **Implementing a Naming Strategy**: Explains how to customize the naming strategy used by Hibernate when mapping Java class names to database table names and field names to column names. - **XML ...
further than the JavaMail API, which offers a protocol-independent model for working with IMAP, POP, SMTP, MIME, and all those other Internet-related messaging protocols. With the help of the ...
The study contributes to the growing field of mobile game development, showcasing how Android SDK can be utilized to create innovative and engaging games that not only entertain but also enrich ...