开发者

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/开发者_StackOverflow中文版_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.


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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜