How to save some values from an array in a controller in Rails?
Posted
by Alfred Nerstu
on Stack Overflow
See other posts from Stack Overflow
or by Alfred Nerstu
Published on 2010-03-11T17:46:06Z
Indexed on
2010/03/11
17:49 UTC
Read the original article
Hit count: 321
I've got a links array that I'm saving to a database. The problem is that the records aren't saved in the order of the array ie links[1] is saved before links[2] and so on...
This is a example from the view file:
<p>
<label for="links_9_label">Label</label>
<input id="links_9_name" name="links[9][name]" size="30" type="text" />
<input id="links_9_url" name="links[9][url]" size="30" type="text" />
</p>
And this is my controller:
def create
@links = params[:links].values.collect { |link| @user.links.new(link) }
respond_to do |format|
if @links.all?(&:valid?)
@links.each(&:save!)
flash[:notice] = 'Links were successfully created.'
format.html { redirect_to(links_url) }
else
format.html { render :action => "new" }
end
end
end
Thanks in advance!
Alfred
© Stack Overflow or respective owner