Ruby on rails Create方法不会将任何记录添加到我的索引列表中
我是rails新手,正在尝试传递crud方法。 我制作了一个Leads模型、控制器、视图和路由 当我提交我的/潜在客户/新页面的表单时,如果我写Ruby on rails Create方法不会将任何记录添加到我的索引列表中,ruby-on-rails,routes,crud,Ruby On Rails,Routes,Crud,我是rails新手,正在尝试传递crud方法。 我制作了一个Leads模型、控制器、视图和路由 当我提交我的/潜在客户/新页面的表单时,如果我写发布“潜在客户/索引” 在我的route.rb文件中,我的浏览器将我引导到索引页,在那里我的新记录不会显示 如果我在路由中跳过上面的代码,我会得到路由错误 控制器: def new @leads = Leads.new end def create @leads = Leads.new(params[:id])
发布“潜在客户/索引”
在我的route.rb文件中,我的浏览器将我引导到索引页,在那里我的新记录不会显示
如果我在路由中跳过上面的代码,我会得到路由错误
控制器:
def new
@leads = Leads.new
end
def create
@leads = Leads.new(params[:id])
if @leads.save
redirect_to(:action => index)
else
render(new)
end
end
resources :leads
<h1>Leads#index</h1>
<%= link_to 'Add new Leads', new_lead_path %>
<% @leads.each do |lead| %>
<tr>
<td><%= lead.name %></td>
<td><%= lead.familyname %></td>
<td><%= lead.mobile %></td>
<td><%= lead.email %></td>
<td><%= link_to 'show' , lead %></td>
</tr>
<% end %>
<h1>Leads#new</h1>
<%= render 'form' %>
路线:
def new
@leads = Leads.new
end
def create
@leads = Leads.new(params[:id])
if @leads.save
redirect_to(:action => index)
else
render(new)
end
end
resources :leads
<h1>Leads#index</h1>
<%= link_to 'Add new Leads', new_lead_path %>
<% @leads.each do |lead| %>
<tr>
<td><%= lead.name %></td>
<td><%= lead.familyname %></td>
<td><%= lead.mobile %></td>
<td><%= lead.email %></td>
<td><%= link_to 'show' , lead %></td>
</tr>
<% end %>
<h1>Leads#new</h1>
<%= render 'form' %>
查看索引:
def new
@leads = Leads.new
end
def create
@leads = Leads.new(params[:id])
if @leads.save
redirect_to(:action => index)
else
render(new)
end
end
resources :leads
<h1>Leads#index</h1>
<%= link_to 'Add new Leads', new_lead_path %>
<% @leads.each do |lead| %>
<tr>
<td><%= lead.name %></td>
<td><%= lead.familyname %></td>
<td><%= lead.mobile %></td>
<td><%= lead.email %></td>
<td><%= link_to 'show' , lead %></td>
</tr>
<% end %>
<h1>Leads#new</h1>
<%= render 'form' %>
Leads#索引
查看新内容:
def new
@leads = Leads.new
end
def create
@leads = Leads.new(params[:id])
if @leads.save
redirect_to(:action => index)
else
render(new)
end
end
resources :leads
<h1>Leads#index</h1>
<%= link_to 'Add new Leads', new_lead_path %>
<% @leads.each do |lead| %>
<tr>
<td><%= lead.name %></td>
<td><%= lead.familyname %></td>
<td><%= lead.mobile %></td>
<td><%= lead.email %></td>
<td><%= link_to 'show' , lead %></td>
</tr>
<% end %>
<h1>Leads#new</h1>
<%= render 'form' %>
Leads#新增
Rails说模型名应该是单数,控制器名应该是复数。如果你遵循它,对你的理解和所有其他人的理解都会更好 首先——桑蒂亚是正确的,你的领导
模型应该是单数的。这将有助于解决路由问题
其次,您正在将params[:id]
传递给Leads.new
,这将使用显式传递的id
生成lead,而不是让ActiveRecord
处理该问题。你想通过(我猜,因为你没有表单
部分代码)params[:lead]
而不是(或者不管表单怎么称呼它)
此外,还有一些小问题,比如调用您创建的单个
Lead
@Lead
(应该是@Lead
)——这不应该导致任何问题,但更符合惯例/更易于阅读 我创建了一个新项目,并更正了模型以引导并更改了创建方法,如下所示def create@Lead=Lead.new(Lead\u params),如果@Lead.save redirect\u to(:action=>'index'),否则渲染('new')end
,方法Lead\u param类似def Lead\u params.require(:Lead)。允许吗(:name,:family,:email,:mobile)end
我想问题在于你提到的这个电话,还有型号名称