2

I want to have multiple forms on one page. Let's make an example to understand what I want:

I have a page for my admins, let's say it's the admins#show page. My admin has to change his name on one form on this page and on another form his age. I know I could create one form but I want to have multiple forms (because this is just an example). So my admins#show page looks something like this:

<%= form_for @admin do |a| %>
    <%= a.label :name %>
    <%= a.text_field :name %>
    <%= a.submit "Submit name change" %>
<% end %>

<%= form_for @admin do |e| %>
    <%= e.label :age %>
    <%= e.number_field :age %>
    <%= e.submit "Submit age change" %>
<% end %>

But in my controller, I don't know really how this works and here is my problem. I think I have something like this, but how could I divide the form inputs in the update method?:

def edit
    @admin = Admin.find(params[:id])
end

def update
    @admin= Admin.find(params[:id])
    if @admin.update_attributes(:name=> admin_params1[:name])
        redirect_to @admin
    else
        render 'edit'
    end

    if @admin.update_attributes(:age=> admin_params2[:age])
        redirect_to @admin
    else
        render 'edit'
    end
end


private

def admin_params1
    params.require(:admin).permit(:name)
end

def admin_params2
    params.require(:admin).permit(:age)
end
ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
Tommy
  • 2,355
  • 1
  • 19
  • 48

2 Answers2

3

Its a bit Unorthodox what you are doing, but as you insisted and only its an example, I guess you can handle the update method by doing like this

def update
  @admin= Admin.find(params[:id])
  if params[:commit] == "Submit name change"
    if @admin.update_attributes(admin_params1)
      redirect_to @admin
    else
      render 'edit'
    end
  elsif params[:commit] == "Submit age change"
    if @admin.update_attributes(admin_params2)
      redirect_to @admin
    else
      render 'edit'
    end
  end
end

Note: Not Tested!

Pavan
  • 33,316
  • 7
  • 50
  • 76
  • 1
    Just a note, :commit is the button name. I am telling this because I couldn't figure out what it is for 10 minutes :D – cyonder Oct 04 '15 at 23:57
2

Well, I think you could create other non-REST methods in the controller and then add named routes in your config/routes then add your two different forms similar to this;

<%= form_for :admin_name, url: admin_name_path, method: :post do |a| %>
    <%= a.label :name %>
    <%= a.text_field :name %>
    <%= a.submit "Submit name change" %>
<% end %>

<%= form_for :admin_age, url: admin_age_path, method: :post do |e| %>
    <%= e.label :age %>
    <%= e.number_field :age %>
    <%= e.submit "Submit age change" %>
<% end %>

Then something like this;

def update_age
    @admin = Admin.find(params[:admin_age][:id])
    if params[:admin_age]
        @admin.update_attributes(:age=> params[:admin_age][:age])
        redirect_to @admin
    else
        render 'edit'
    end

end

def update_name
    @admin = Admin.find(params[:admin_name][:id])
    if params[:admin_name]
        @admin.update_attributes(:name=> params[:admin_name][:name])
        redirect_to @admin
    else
        render 'edit'
    end

end

** not tested for bugs

cweston
  • 11,297
  • 19
  • 82
  • 107
acacia
  • 1,375
  • 1
  • 14
  • 40