我有以下
这些模型之间没有任何关系。
我希望在页面的显示模板上有Form_for的
这可能吗?
目前有以下内容:
<div id="sidebar">
<%= form_for (@business) do |f| %>
<div id="contact_form_name">
<p>Company</p>
<%= f.text_field :company_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
<div id="sidebar">
<%= form_for (@client) do |f| %>
<div id="contact_form_name">
<p>First Name</p>
<%= f.text_field :first_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
我在日志中的错误是以下
<div id="sidebar">
78: <%= form_for (@business) do |f| %>
79: <div id="contact_form_name">
80: <p>Company</p>
81: <%= f.text_field :company_name, :class =>'form_input_small' %>
app/views/pages/show.html.erb:78:in `_app_views_pages_show_html_erb___1556847543_65073939124600'
app/controllers/pages_controller.rb:9:in `show'
路线
businesses GET /businesses(.:format) {:action=>"index", :controller=>"businesses"}
POST /businesses(.:format) {:action=>"create", :controller=>"businesses"}
new_business GET /businesses/new(.:format) {:action=>"new", :controller=>"businesses"}
edit_business GET /businesses/:id/edit(.:format) {:action=>"edit", :controller=>"businesses"}
business GET /businesses/:id(.:format) {:action=>"show", :controller=>"businesses"}
PUT /businesses/:id(.:format) {:action=>"update", :controller=>"businesses"}
DELETE /businesses/:id(.:format) {:action=>"destroy", :controller=>"businesses"}
答案 0 :(得分:1)
假设您正在使用RESTful资源(正如您所希望的那样),您将拥有以下内容:
resources :pages
resources :companies
resources :clients
在routes.rb中设置,这使得指定form_fors的运行方式非常容易。
例如,对于您的Page模型的show动作,您可以使用以下内容:
<h1>New Company:</h1>
<%= form_for @company, :url => companies_path do |f| %>
...
<% end %>
<h1>New Client:</h1>
<%= form_for @client, :url => clients_path do |f| %>
...
<% end %>
确保您在页面控制器显示操作中设置实例变量@company
和@client
,如@company = Company.new
&amp; @client = Client.new
。
在这两种情况下,您的表单都会发布到各自模型的创建操作中。您可以查看relying on record identification以进一步阅读。