form_with scope

About scope of form_with

There was such a form, but what did scope mean?

ruby.rb


<%= form_with scope: :session, url: sessions_path do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

Let's take a look at the verification tool for the time being I see, it works like this.


<form action="/sessions" method="post" data-remote="true">
  <input type="text" name="session[name]">
</form>

So what does the scope option do?

The value given to the spope object is the prefix of the name value

name = "session[name]"

The parameters are sent in the form of. That

How do you receive it?

It's not difficult to think because session is just added to the parameter prefix.

controller.rb



def create
  #Session the value sent using the scope[:user_id]Substitute in
  session[:user_name] = session_params[:name]
end

It means that it works like this. It wasn't a big deal.

That's all for today. form_with There are so many different things.

*** 87 days to become a full-fledged engineer ***

Recommended Posts

form_with scope
Scope
About scope
[Rails] form_with
Java variable scope (scope)
Refactoring with scope
Variables / scope (ruby)
Java variable scope
Studying Java-Part 9-Scope
Java session scope
Corresponds to Scope