Need Help With Ruby On Rails Development?

Work with our skilled Ruby on Rails developers to accelerate your project and boost its performance.

Hire Ruby on Rails Developer

Support On Demand!

1. What does the :as => :user say ?

The :as => :user option is used to define the parameter name in the form submission. For example:

<%= form_for @user, :as => :user, :url => sign_in_path(@user) do |f| %>

When the form is submitted, Rails will group the submitted fields under a key named user. For instance:

Parameters: { "user" => { "email" => "test@example.com", "password" => "password" } }
If you leave out :as, Rails will infer the name based on the model (e.g., @user defaults to user).

you could use :as => :custom_key if you wanted the submitted parameters to be nested under params[:custom_key] instead of params[:user]

2. :url => sign_in_path is clear, but why is there a (@user) behind it?

sign_in_path is a Rails route helper, generating a URL for a specific action.
@user is passed as an argument to include information about the @user object in the URL.

#routes.rb
get 'sign_in/:id', to: 'sessions#new', as: 'sign_in'

In this case, sign_in_path(@user) generates a URL like /sign_in/123, assuming @user.id is 123.

3. how to access @user object in application.html.erb

Solution: Use a before_action in ApplicationController
The ApplicationController is a base class for all controllers in your Rails application, so adding a before_action here will make @user available across all views, including layouts.

class ApplicationController < ActionController::Base
  before_action :set_user
  private
  def set_user
    # Assuming current_user is a method that returns the logged-in user
    @user = current_user
  end
end

4. <%= form_for (User.new) works well, but it isn't right.

It’s not ideal for Clarity and Readability, Controller Responsibility, Consistency
A Better Approach:
Initialize the model object in the controller and use the instance variable in the view.
Controller:-

class UsersController < ApplicationController
  def new
    @user = User.new
  end
end

View:-

<%= form_for @user do |f| %>
  
<% end %>

Benefits of This Approach:

Encapsulation : The controller is responsible for preparing the data, and the view focuses on rendering it.

Flexibility: If you need to modify how @user is initialized (e.g., with default attributes), you only update the controller.

Testing: Makes your views easier to test because the required data is explicit.

5. Why is there something like a for/forEach-loop? do |f| %>

form_for: This helper is used to create a form for a specific model object. It binds the form to the model and allows Rails to manage form fields and parameters efficiently.
Block: The do ... end block (or { ... } in shorter form) is passed to the form_for helper, and inside the block, you define the fields and structure of the form.
-> f: The form builder object provided by form_for. You use this object to generate fields like text_field, password_field, etc.

# Syntax
<%= form_for @user do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name %>
   <%= f.label :email %>
    <%= f.email_field :email %>
    <%= f.submit "Save" %>
<% end %>

Related Q&A