Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rails for Beginners Part 13: Creating a Sign Up Form Discussion

Plural/singular confuses me. I know Models are singular ("User"). Why "Registrations" (plural) for the controller? Does it matter for controllers which aren't directly associated with a Model? Looking at your source, I see many of the views are plural also. Any tips for keeping this straight?

Reply

Here are few naming conventions on models, controllers, views, routes
https://gist.github.com/iangreenleaf/b206d09c587e8fc6399e

Usually models are singular and controllers plural. A User model has features, validations etc which define a user. A UsersController handles/controls all users.

However, in case of controllers and views they take singular names when they are not associated with models or do not abstract a resource e.g WelcomeController.
I think with practice this naming will come intuitively.

Reply

Thanks for the resource link! : )

Reply

Could you clarify why "model: @user" instead of "model: User"? Why the instance variable instead of the Model name? Thank you.

Reply

AFAIK, that's because you need to pass an instance of a class not the class. So that, the form will fill out the missing parts of that specific instance upon submission of the form and save into the DB.

Reply

Thank you. That helps a little I guess. :-) So a generic instance of the class has been created as a Ruby object with no specific properties via the "new" method, and then "create" inserts the user's values and attempts to save it? Still hasn't clicked in but I guess it makes sense the more you do it.

Reply

We get to the 'Sign up' form from the registrations_controller#new action which defines the @user instance variable as User.new (calling the new method on the User model). Because we use an instance variable in the controller, it is available to be used in the 'View' (new.html.erb) where we create the form. form_with is a form helper that will generate a form tag and create a 'POST' action when submitted to the URL that we define, which, in this case is routed to our registrations_controller#create action which will create a new user in the database using the fields that are passed to it in the form fields (email, password, password_confirmation) : )

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.