ruby - rails 开发 保存对象报错?
ringa_lee
ringa_lee 2017-04-24 09:12:48
0
1
781

目的:实现通过前段网页提交并保存一个一个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
ringa_lee
ringa_lee

ringa_lee

Antworte allen(1)
大家讲道理

遇到这类问题,我一般对项目全局搜索 lname 一定是哪里代码多输了一个 l 找出来修改掉就行了~

希望有所帮助~ :)

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage