[RUBY] [Rails] How to get the contents of strong parameters

Overview

How to write a process to extract the contents of strong parameters in Rails

Situations that use this method

Editing screen ↓ Editing content confirmation screen ↓ Completion screen

For some reason, in the screen transition When you want to display the value of the strong parameter on the edit content confirmation screen, etc.

Prior knowledge

What are strong parameters? https://qiita.com/ozackiee/items/f100fd51f4839b3fdca8

Implementation example

Editing screen

Ruby:user_edit.html.erb


<!--name and email are submitted from the input form to the confirmation screen-->
<!--It is an edit about the created user, and its ID value shall be passed separately as a URL parameter.-->
<%= form_with(model:@user, url: confirm_user_path) do |f| %>
<div class="field">
  <%= f.text_field :name, placeholder: "Enter your username!" %>
</div>
<div class="field">
  <%= f.text_field :email, placeholder: "e-Please enter your mail address!" %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

Strong parameters in the controller

UsersController.php




//Define strong parameters
private
 def user_params
            params.require(:user).permit(:name, :email)
 end

Variable definition for confirmation screen display in the controller

UsersController.php



def edit_confirm
    @user = User.find(params[:id])  #Get user data with the ID obtained from the URL parameter(Example: users/3/edit_confirm)
    @user.name = user_params[:name] #Extracting the value of a strong parameter & assigning it to a variable
    @user.email = user_params[:email] #Same as above
end

confirmation screen

Ruby:edit_confirm.html.erb


    <h2>Confirmation of edited contents</h2>
    <p>Full name:<%= @user.name %>Mr.</p>
    <p>mail address:<%= @user.email %></p>

Why i wrote this article

Regarding how to create a "confirmation screen", in Qiita articles etc., it is often

UsersController.erb


@user = User.new(user_params)

And set it on the template side

Ruby:edit_confirm.html.erb


    <h2>Confirmation of edited contents</h2>
    <p>Full name:<%= @user.name %>Mr.</p>
    <p>mail address:<%= @user.email %></p>

But in my case Because I set a certain validation for the create action When I ran User.new, it was recognized as a create action and I was getting a validation error. I wonder if strong parameters can be used to display the input contents on the confirmation screen while avoiding it. When I tried it, I was able to get it and assign it to a variable by the above method.

I'm glad if you can use it as a reference.

Recommended Posts

[Rails] How to get the contents of strong parameters
[Rails] How to get the URL of the transition source and redirect
How to get the contents of Map using for statement Memorandum
Customize how to divide the contents of Recyclerview
How to get today's day of the week
[Java] How to get the authority of the folder
[Ruby On Rails] How to search the contents of params using include?
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
[Swift] How to get the document ID of Firebase
[Rails] Check the contents of the object
How to get parameters in Spark
[Rails] Add strong parameters to devise
How to get along with Rails
How to get the longest information from Twitter as of 12/12/2016
[Ruby] How to retrieve the contents of a double hash
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
How to change the contents of the jar file without decompressing
[jsoup] How to get the full amount of a document
[Rails] How to use the map method
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to determine the number of parallels
[Java] How to get the current directory
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to change the page title of the browser for each page
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Rails] Read the RSS of the site and return the contents to the front
How to get the date in java
The process of introducing Vuetify to Rails
How to check for the contents of a java fixed-length string
How to get the length of an audio file in java
How to decorate the radio button of rails6 form_with (helper) with CSS
[Rails] How to get the user information currently logged in with devise
How to solve the local environment construction of Ruby on Rails (MAC)!
How to get the absolute path of a directory running in Java
[For Rails beginners] Summary of how to use RSpec (get an overview)
How to make the schema of the URL generated by Rails URL helper https
[Rails] How to solve the time lag of created_at after save method
[Rails] How to decide the destination by "rails routes"
[Rails] Button to return to the top of the page
[Rails] Introduction of pry-rails ~ How to debug binding.pry
I want to var_dump the contents of the intent
Output of how to use the slice method
How to use JQuery in js.erb of Rails6
How to display the result of form input
[Java] How to get the redirected final URL
[Rails] How to get success and error messages
How to check Rails commands in the terminal
How to write Rails
Summary of strong parameters
How to uninstall Rails
About rails strong parameters
[Swift] How to get the number of elements in an array (super basic)
graphql-ruby: How to get the name of query or mutation in controller Note
How to get the ID of a user authenticated with Firebase in Swift
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Rails] How to get rid of flash messages in a certain amount of time
[Java] How to easily get the longest character string of ArrayList using stream
How to make a unique combination of data in the rails intermediate table