目的:实现通过前段网页提交并保存一个一个company对象,这个对象只有一个字段,那就是name.
但是提交时保存却说:找不到lname方法!controller保存时并没有设计lname方法,不知道为什么会报这个错误。
2天了,没弄明白。
下面是报错:
NoMethodError in CompaniesController#create
undefined method `lname' for #<Company id: nil, name: "c1", created_at: nil, updated_at: nil>
Extracted source (around line #23):
21 def create
22 @company = Company.new(params[:company].permit(:name))
23 @company.save
24 redirect_to @company
25 end
Rails.root:
Application Trace | Framework Trace | Full Trace
app/controllers/companies_controller.rb:23:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"3DISc2IKH23W5KUHORMza5NHm9eMq35KDdbrbus6LqDXNVGVku0vsI2QyJ2LopGD1TlMgR+3w8N0QqtTJ9boMQ==",
"company"=>{"name"=>"c1"},
"commit"=>"Create Company"}
views/compines/new.html.erb
<h1>New Company</h1>
<%= form_for(@company) do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit "Create Company" %>
</p>
<% end %>
<% content_for :aside do %>
<% render 'sidebar' %>
<% end %>
companies_controller.rb
def create
@company = Company.new(params[:company].permit(:name))
@company.save
redirect_to @company
end
When encountering this kind of problem, I usually search the project globally
lname
一定是哪里代码多输了一个l
and just find it and modify it~Hope it helps~ :)