[RUBY] Rails Review 2

Purpose of this post I will write it as minutes to make a note of Rails operation. This is a continuation of [Rails Review 1](https://qiita.com/naoto911/items/f46b35f84cc80f59cac3) that I wrote earlier.

Teaching materials used for learning Udemy's ["Introduction to Ruby on Rails-Learn Ruby and Rails from the Basics and Publish Web Applications to the Net"](https://www.udemy.com/course/the-ultimate-ruby-on-rails- I used bootcamp /) as a teaching material.

① How to use link_to

qiita.rb


link_to 'Link name', 'Prefix name+ _path'

・ View helper ・ Automatically creates html (creates href)

○ Link name ⇨ Describe that it is displayed as a character string in view

○'Prefix name + _path' ⇨ Use rails routes Prefix (requires _path)

[How to write URL pattern with id] ・ Focus on the URL pattern associated with Prefix -The URL pattern may contain variables that are not uniquely defined, such as: id. ・ * (When it is uniquely determined, only the above description is OK)

·in this case,'Prefix + _path + (each do | |Value generated by)'Described as ・ Each do| |The value generated by,Generated from instance variables ・ * (Assumption that instance variables have been generated by controller)

qiita.rb


@Instance variable each do|Instance name|

-In this instance name, multiple caches of instance variables are stored. -Convert the value where column name = id to: id in the URL pattern -It means that the: id of the url is described with the argument: id included.

For example

Prefix          URL Pattern
edit_question   question/:id/edit

-The URL pattern associated with this Prefix includes: id -Therefore, it is necessary to describe (instance name) as well.

qiita.rb


<% @question each do |question| %>
<%= link_to 'Edit', edit_question_path(question) %>
<% end %>

[Explanation] -[: Id,: name,: title,: content] is stored in the question, which is the instance name. ・ Of these,: id is automatically picked up and applied to the URL pattern.

② How to use redirect_to

qiita.rb


ridirect_to 'Prefix name+ _path'

-Method to move to the specified URL ・ Basically, the idea is the same as ① link_to method. ・ Differences are described on the Controller -Therefore,'link name' is unnecessary because it is not displayed in view. -Also, the instance variable itself can be used when prefixing the URL pattern. ・(Instance variable each do for description in Class|Instance name|Is unnecessary)

[How to write URL pattern with id]

qiita.rb


def action
    @Instance variables=Model name.find(params[:id])
    redirect_to question_path(@question)
  end

[Explanation] ・ Please think that pramasun is a hash name that is summarized separately and pass it through. ・ Get: id from @question and apply it to the URL pattern

③ How to use render

qiita.rb


render :html file name

・ Method to skip to URL as in ①② -Note that specify the name of the html file instead of Prefix with: html file name. ・ Basically used in actions -Used when the process at the time of action execution is famous -Return to the path before accessing the path of the action execution condition

qiita.rb


def action
    @Instance variables=Model name.find(params[:id])
    redirect_to question_path(@question)
  end

④ How to use redirect_to and render -Here, an example is explained when the URL pattern is uniquely determined by Prefix.

qiita.rb


  def create
    @question = Question.new
    if @question.save
      redirect_to root_path, notice: 'Success!'
    else
      flash[:alert] = 'Save error!'
      render :new
    end
  end

[Explanation] -Branch the URL that goes after the action is executed into two (redirect_to side or render side) ・ Instantiate @question from Question model ・ Hold the value with @ question.save ・ If the value can be saved correctly here, move to the prefix of redirect_to. ・ If saving fails, return to new.html.erb

○notice: 'Success!' ⇨ When accessing the URL after redirect_to, the user is displayed as'Success!'

○flash[:alert] = 'Save error!' ⇨ "Save error!" Is displayed to the user on the returned view screen.

Recommended Posts

Rails Review 1
Rails Review 2
[Rails g.error]
Rails API
Rails migration
[Rails] first_or_initialize
Rails book review app RSpec introduction
About Rails 6
Java review
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
Encapsulation review
rails tutorial
[Rails] devise
rails tutorial
rails tutorial
rails method
rails tutorial
Ruby Review 1
[Rails] ActiveRecord
[Rails] form_with
Rails6 jQuery introduced
Review about bundler
About Rails routing
Add binding.pry (rails)
Rails database basics
Rails access restrictions
Rails and FormData
rails tutorial Chapter 6
Rails tutorial test
[Rails] Favorite feature
Rails Bootstrap introduced
[Rails] Many-to-many creation
[Rails] Naming convention
Rails Credentials Note
Rails Logger Basics
[Rails] Introducing jquery
[Rails 6] Ranking function
Rails Flash Message
Tailwind on Rails
rails tutorial Chapter 1
[Rails 6] cocoon_ introduction
[Rails] Introduce Carrierwave
Rails tutorial memorandum 1
[rails] Set validation
Rails learning day 3
Enum review notes
Rails tutorial memorandum 2
Rails learning day 4
Implement Rails pagination
[Rails] Category function
rails tutorial Chapter 7
[Rails] Introducing devise
rails tutorial Chapter 5
Group_by in Rails
Rails follow function
rails tutorial Chapter 10
[Rails] Extend UrlHelper
rails + devise + devise_token_auth