Search code examples
ruby-on-railsslim-lang

Proper syntax for controller action with slim


Rails 3.2

I have a view views/tickets/show.html.slim with a number of sections. I want to have different controllers for each section, and have actions like New Save Edit

So in my views/tickets/show.html.slim, I have:

- @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 }

In my view, I have:

= 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

When I run the application in test mode, and I select a ticket, I get the following response:

Incomplete response received from application

In my test.log file, I have:

CustomerInfo Load (0.1ms)[0m SELECT customer_infos``.* FROMcustomer_infosWHEREcustomer_infos.ticket_id` = '1466026127' LIMIT 1 Marker 1 Rendered tickets/sections/_customer_info.html.slim (11.6ms) Rendered admin/tickets/show.html.slim within layouts/application (563.0ms)

There is no Marker 2

If I replace:

.actions = link_to "Save", :controller => :customer_infos, :action => :create

With:

    .actions = f.submit 'Save'

Then the form renders fine.

Any idea why this is not working?

Attempted fix:

In my tickets/section/_customer_info.html.slim, I did:

.actions = link_to "Save", create_customer_info_path, method: :post

In my routes.rb, I have:

post '/customer_infos/create' => 'customer_infos#create', as: 'create_customer_info'

I am now getting the following error message:

undefined method `customer_infos_path' for #<#<Class:0x00000008bb54d8>:0x00000009df3c30>

Where is customer_infos_path coming from?

If I do rake routes, I get:

create_customer_info POST /customer_infos/create(.:format) customer_infos#create

Solution

  • According to the Rails docs, using the controller option is frowned upon. You should set up your route with a name, like this:

    post '/customers/create' => 'customer_infos#create', as: 'create_customer'
    

    Then your view should look something like this:

    .actions
        = link_to "Save", create_customer_path, method: :post
    

    Hope this helps!