Create mutiple row one time and save into database.

40 views
Skip to first unread message

Nikhil Thombare

unread,
Dec 13, 2014, 2:01:42 AM12/13/14
to rubyonra...@googlegroups.com
{"utf8"=>"✓",
"authenticity_token"=>"fB/ZNf3dnFz9OVa/h34xbNysN0AlxcIiPs=",
"f"=>[{"columns_id"=>"4",
"column_value"=>"rrr",
"users_id"=>"2"},
{"columns_id"=>"5",
"column_value"=>"ttt",
"users_id"=>"2"}],
"commit"=>"Save changes"}


I want to store object values but how to store multiple record at same
time using controller.

my controller code -

def create
params[:f].each do |user_params|
Detail.new(user_params).save
end
end


my view code -

<%= form_for(:detail, :url => {:controller => 'details', :action =>
'create'}) do %>
<% @info.each do |article| %>
<%= hidden_field_tag "f[][columns_id]", article.id %>
<p>
<td><%= article.value %></td>
<td><%= text_field_tag "f[][column_value]" %></td>
</p></br>
<%= hidden_field_tag "f[][users_id]", current_user.id %>
<% end %>
<p class="button"><%= submit_tag %></p>
<% end %>

Its gives error ActiveModel::ForbiddenAttributesError how to solve
please help me

--
Posted via http://www.ruby-forum.com/.

mode-x

unread,
Dec 13, 2014, 7:04:45 PM12/13/14
to rubyonra...@googlegroups.com
You cannot use params[:f] that way.

dasibre

unread,
Dec 15, 2014, 8:58:43 AM12/15/14
to rubyonra...@googlegroups.com
You want to store what object values? Is it just one object or two related objects?Try using this rails convention http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

 Make your life easier, stick to rails conventions. 

Dennis Krupenik

unread,
Dec 15, 2014, 6:07:21 PM12/15/14
to rubyonra...@googlegroups.com
I suggest you use a Form Object for this: http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/#form-objects
It's not that our form is overly complex, but it falls outside of rails conventions.
Anyway, please, do not put such logic in the controller, make a static method on the model at least.
Reply all
Reply to author
Forward
0 new messages