简体   繁体   English

Rails中新的关联对象的渲染形式

[英]Rendering form for new associated object in rails

I am having trouble with creating association; 我在建立联系方面遇到麻烦;

My models are Table and Columns (one table to many columns). 我的模型是表和列(一个表到许多列)。

In my ColumnsController: 在我的ColumnsController中:

def new
    @table = Table.find(params[:id])
    @column = @table.columns.build
end  

In my app/views/tables/show.html.erb: 在我的app / views / tables / show.html.erb中:

<%= @table.name %></dd>
<%= render "columns/form" %>

In my app/views/columns/_form.html.erb: 在我的app / views / columns / _form.html.erb中:

<%= form_for  @column, :url => {:action => :create, :id => @table.id }do |f| %>
<%= f.label :name  %>
<%= f.text_field :name %>
<%= f.button :submit%><% end %>

When I run it: 当我运行它时:

undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
<%= form_for  @column, :url => {:action => :create, :id => @table.id } do |f| %>

Why ? 为什么呢 :-( :-(

Try doing it with partials and local variables: 尝试使用局部变量和局部变量:

<%= render partial: "columns/form", locals: {table: @table, column: @column} %>

and

<%= form_for  column, :url => {:action => :create, :id => table.id }do |f| %>

If you want it even simpler, you can do this: 如果您想要更简单,可以执行以下操作:

<%= render partial: "columns/form", locals: {table: @table} %>

and

<%= form_for table.columns.new do |f| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM