Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 Forms from seperate controller in 1 view

I have 2 controllers: models_controller and portfolios_controller

Rather than going to separate pages to fill in these forms, I'd like to display all of the forms in a single view and fill in all of the data at once.

Here's the 2 in 1 form I'm trying to get to work: https://github.com/imjp/models/blob/master/app/views/models/_form.html.erb

What is the simplest way to achieve my goal according to current best practices?


EDIT 1: I've found a way to display the forms together in 1 view but there one problem, the submit button from the original form is throwing up errors if it needs to throw up validation errors: undefined method 'model_name' for NilClass:Class

Take a look at the link provided above.

like image 340
imjp Avatar asked Feb 15 '26 00:02

imjp


1 Answers

since model :has_one portfolio, the rails best practices is nested_attributes

#Model (https://github.com/imjp/models/blob/master/app/models/model.rb)
class Model < ActiveRecord::Base
...
  accepts_nested_attributes_for :portfolio
...
end


#View (https://github.com/imjp/models/blob/master/app/views/models/_form.html.erb)
<% fields_for :portfolio |portfolio_form| %>
  ... 
  <div class="field">
    <%= portfolio_form.label :model_id %><br />
    <%= portfolio_form.number_field :model_id %>
  </div>
  ...
<% end %>
like image 106
Anatoly Avatar answered Feb 17 '26 13:02

Anatoly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!