I've looked at these two SO discussions yet it's not working for me
1) Creating multiple records at once from comma seperated IDs
2) How to accept comma-delimited list to build tags for model?
I am trying to take a single string from a form and create multiple active record entries being separated by commas
form:
<%= form_for @tag, html: {:class => "form-horizontal" } do |f| %>
<fieldset>
<div class="form-group">
<%= f.label :tagname, "Tag Name", :class => "col-lg-3 control-label" %>
<%= f.text_field :tagname, :class => "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: 'btn btn-primary' %>
</div>
<%= link_to 'Back', tags_path %> |
<%= link_to 'Home', :userroot %>
</fieldset>
<% end %>
controller:
def create
@tag = params[:tagname].split(/,\s*/)
@tag.each do |t|
newtag = Tag.new(:tagname => t)
newtag.save
end
redirect_to tags_path
end
error:
NoMethodError in TagsController#create
undefined method `split' for nil:NilClass
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"RwjDJ9WQD6nJ51AymbSBubcmDbzDNTYWot0nP1FPV04=",
"tag"=>{"tagname"=>"a new tag, two tag, three tag"},
"commit"=>"Create Tag"}