When using Slim in Ruby on Rails, "undefined local variable or method` f'for "is displayed in form_with

Development environment

Ruby: v2.5.7 Rails: v5.2.4.4 gem slim: v4.1.0

error contents

= form_with model:@team, local:true do |f|

= render 'layouts/error_message', model: f.object

.input
  = f.label :name
  = f.text_field :name

.input
  = f.label :introduction
  = f.text_area :introduction

  = f.submit

This time, I introduced slim which is one of the Gem of Ruby on Rails, but I got an error immediately. The content is![Screenshot 2020-10-11 12.24.41.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/613376/1675190f-a192-b820 -8887-b51b14351f45.png) With NameError undefined local variable or method f' for #<#<Class:0x00007fef406b53c8>:0x00007fef35880668>

It means that the specified f is not defined. Even though the block variable described in form_with says f ... I wondered if there was a way to write form_with peculiar to slim, so I was researching various things. As a result, it was solved by correcting as follows.

= form_with model: @team, local: true do |f|

/Each paragraph indented
  = render 'layouts/error_message', model: f.object

  .input
    = f.label :name
    = f.text_field :name

  .input
    = f.label :introduction
    = f.text_area :introduction

    = f.submit

It was an indentation adjustment ... it's simple to understand ...

I didn't need ʻend or () `, so I was wondering how to determine the scope of application of form_with, but it is identified by indentation. So that's it... I noticed through this error. Lol

It's a simple thing, but I'm addicted to it, so I'll post it.

Even so, it takes some getting used to, but even if you don't need a few descriptions and parentheses, the description is considerably simplified and the code is easy to write and easy to read. In addition, it seems that rendering is a little faster, so it's just a good point, slim.

Recommended Posts

When using Slim in Ruby on Rails, "undefined local variable or method` f'for "is displayed in form_with
When using Slim in Ruby on Rails, "undefined local variable or method` f'for "is displayed in form_with
I get an error with bundle install and puma cannot be installed.
Undefined local variable or method error in partial template collection option
Be absolutely careful when putting the result of and / or in a variable!
I got an error "undefined method` create'" in RSpec
rspec Error: ActionView :: Template :: Error: undefined method `name'for nil: NilClass
[Ruby] undefined method `dark?'occurs in rqr_code
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
[Ruby on Rails] Carousel of bootstrap4 is implemented as a slide show using each method.
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
[Ruby on Rails] Cookie is used and displayed only once at the first access (using jquery.cookie.js)
Ruby on Rails variable, constant summary
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to create a query using variables in GraphQL [Using Ruby on Rails]
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
What to do when Blocked Host: "host name" appears in Ruby on Rails
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
[Ruby on Rails] Follow function undefined method ʻid'for nil: NilClass error resolution
[Ruby on Rails] Code check using Rubocop-airbnb
Ruby on Rails installation method [Mac edition]
Ruby on Rails in Visual Studio Codespaces
Notes on using FCM with Ruby on Rails
[Rails] About local: true described in form_with
Beginners create portfolio in Ruby on Rails
When I run minitest in vscode's Ruby Test Explorer, NoMethodError: undefined method `klass'for ...
How to implement image posting function using Active Storage in Ruby on Rails