mardi 25 août 2015

Submit form with

I need a way to save 2 or more models in one form. Mymodel has two inputs (s_url and i_url). Here is a test form that I set up.

<%= form_for :mymodel, :url => mymodels_path do |f| %>
    <p>
      <%= f.text_field :s_url %><br />
      <%= f.text_field :i_url %>
    </p>

    <p>
      <%= f.text_field :s_url %><br />
      <%= f.text_field :i_url %>
    </p>
    <p><%= f.submit "Submit" %></p>
<% end %>

And here is the Controller:

def new
    @mymodel = Mymodel.new
end

 def create
    @mymodel = Mymodel.new(s_url: params[:s_url], i_url: params[:i_url])
    if @mymodel.save
      redirect_to some_index_path(@some_index, :anchor => params[:s_url])
    end
  end

When I save, it only submits one of the models instead of both. Also, I don't know how many models the user will save. I just hardcoded 2 into the form in order to test if they would both send. But in the end, the form will be dynamic.

Aucun commentaire:

Enregistrer un commentaire