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

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

membalas semua(1)
大家讲道理

Apabila menghadapi masalah seperti ini, saya biasanya mencari projek secara global lname Mesti ada kod tambahan yang hilang di suatu tempat l Cuma cari dan ubah suainya~

Semoga ini membantu~ :)

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan