How to save some values from an array in a controller in Rails?
- by Alfred Nerstu
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