开发者

Ruby on Rails - Multiple values for form data

I'm new to rails and I'm looking to make a simple page that allows a user to list their computer skills.

I need fields to be dynamically created by clicking 'Add anoth开发者_Python百科er skill.' I've found some JS that can do this, but I'm having trouble getting the multiple fields stored in a single database entry in Rails.

Any ideas?

--

For example:

Skills

-MS Office

-Mac OS X

-AutoCad

-StackOverflow

Add Another Skill


Your best option is to add an entry in a new table for each skill, instead of a single database entry.

Steps involved in this:

  1. You would have a table called 'Skills' that would have a primary id and text field for the text.
  2. Add in a has_many :skills to your 'User' object.
  3. In the form, each added item would have the name something like: user[skills][]. This will add create skill objects when posted to the server.

If you need to keep with a single database entry you could YAML to serialize and de-serialize an array of skills (not tested much).

require 'yaml'

def update

   user.skills = ["MS Office", "Mac OS X"].to_yaml

end

And in the template:

<% YAML::load(user.skills).each do |s| %>
   <%= s %>
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜