使用 slim 的控制器操作的正确语法
Proper syntax for controller action with slim
Rails 3.2
我有一个包含多个部分的视图 views/tickets/show.html.slim。我想为每个部分设置不同的控制器,并进行类似 New Save Edit
的操作
所以在我的 views/tickets/show.html.slim 中,我有:
- @customer_info = customer_info @ticket
h4.form-header Customer Information
.form-section.attachments
- if @customer_info.nil?
= render partial: 'tickets/sections/customer_info', locals: {ticket: @ticket }
在我看来,我有:
= form_for CustomerInfo.new do |f|
- f.hidden_field :ticket_id, :value => ticket.id
.form-horizontal-column.customer-info
.form-group
= f.label :first
= f.text_field :first, maxlength: 50
.form-group
= f.label :last
= f.text_field :last, maxlength: 50
- logger.info("Marker 1")
.actions = link_to "Save", :controller => :customer_infos, :action => :create
- logger.info("Marker 2")
.clear
当我 运行 应用程序处于测试模式时,我 select 一张票,我得到以下响应:
Incomplete response received from application
在我的 test.log 文件中,我有:
客户信息加载(0.1 毫秒)[0m SELECT customer_infos``.* FROM
customer_infosWHERE
customer_infos.
ticket_id` = '1466026127' 限制 1
标记 1
呈现 tickets/sections/_customer_info.html.slim(11.6 毫秒)
在 layouts/application(563.0 毫秒)
内呈现 admin/tickets/show.html.slim
没有标记 2
如果我替换:
.actions = link_to "Save", :controller => :customer_infos, :action => :create
与:
.actions = f.submit 'Save'
然后表单呈现正常。
知道为什么这不起作用吗?
尝试修复:
在我的 tickets/section/_customer_info.html.slim 中,我做了:
.actions = link_to "Save", create_customer_info_path, method: :post
在我的 routes.rb 中,我有:
post '/customer_infos/create' => 'customer_infos#create', as: 'create_customer_info'
我现在收到以下错误消息:
undefined method `customer_infos_path' for #<#<Class:0x00000008bb54d8>:0x00000009df3c30>
customer_infos_path 来自哪里?
如果我做rake路线,我得到:
create_customer_info POST /customer_infos/create(.:format) customer_infos#create
根据 the Rails docs,使用 controller
选项是不受欢迎的。您应该使用名称设置您的路线,如下所示:
post '/customers/create' => 'customer_infos#create', as: 'create_customer'
那么您的视图应该如下所示:
.actions
= link_to "Save", create_customer_path, method: :post
希望对您有所帮助!
Rails 3.2
我有一个包含多个部分的视图 views/tickets/show.html.slim。我想为每个部分设置不同的控制器,并进行类似 New Save Edit
的操作所以在我的 views/tickets/show.html.slim 中,我有:
- @customer_info = customer_info @ticket
h4.form-header Customer Information
.form-section.attachments
- if @customer_info.nil?
= render partial: 'tickets/sections/customer_info', locals: {ticket: @ticket }
在我看来,我有:
= form_for CustomerInfo.new do |f|
- f.hidden_field :ticket_id, :value => ticket.id
.form-horizontal-column.customer-info
.form-group
= f.label :first
= f.text_field :first, maxlength: 50
.form-group
= f.label :last
= f.text_field :last, maxlength: 50
- logger.info("Marker 1")
.actions = link_to "Save", :controller => :customer_infos, :action => :create
- logger.info("Marker 2")
.clear
当我 运行 应用程序处于测试模式时,我 select 一张票,我得到以下响应:
Incomplete response received from application
在我的 test.log 文件中,我有:
客户信息加载(0.1 毫秒)[0m SELECT customer_infos``.* FROM
customer_infosWHERE
customer_infos.
ticket_id` = '1466026127' 限制 1
标记 1
呈现 tickets/sections/_customer_info.html.slim(11.6 毫秒)
在 layouts/application(563.0 毫秒)
没有标记 2
如果我替换:
.actions = link_to "Save", :controller => :customer_infos, :action => :create
与:
.actions = f.submit 'Save'
然后表单呈现正常。
知道为什么这不起作用吗?
尝试修复:
在我的 tickets/section/_customer_info.html.slim 中,我做了:
.actions = link_to "Save", create_customer_info_path, method: :post
在我的 routes.rb 中,我有:
post '/customer_infos/create' => 'customer_infos#create', as: 'create_customer_info'
我现在收到以下错误消息:
undefined method `customer_infos_path' for #<#<Class:0x00000008bb54d8>:0x00000009df3c30>
customer_infos_path 来自哪里?
如果我做rake路线,我得到:
create_customer_info POST /customer_infos/create(.:format) customer_infos#create
根据 the Rails docs,使用 controller
选项是不受欢迎的。您应该使用名称设置您的路线,如下所示:
post '/customers/create' => 'customer_infos#create', as: 'create_customer'
那么您的视图应该如下所示:
.actions
= link_to "Save", create_customer_path, method: :post
希望对您有所帮助!