Progate Ruby on Rails5 Looking Back

Progate Ruby on Rails Course Impressions

I spent about 200 hours learning programming and embarked on a Rails course. Although the learning so far has progressed smoothly, this time it was a difficult time. I do not fully understand the contents of Dojo Course 4. Keep a record as a memo so that you can review it.


Migration file related

What is a migration file: A blueprint for creating a database. What is a model: A special class for manipulating tables    Creating a model

rails g model Post content:text 

rails g model model name column name: data type

rails g migration add_image_name

Get the value from the URL

Specify the hash in the routing.

routes.rb


get "posts/:id" => "posts#show"

Can be obtained by using params [hash]

controller.rb


def show
  @id = params[:id]
end

Form submission destination settings

erb:posts/new.html.erb


<%= form_tag("/posts/create") do%>
<% end %>

Don't forget do

Link description method at the time of post

Specify the method post as the third argument.

<%= link_to("delete","/posts/#{@post.id}/destroy",{method: "post"})%>
<% end %>

Branching in the process of saving to the database

Note that the save process is executed in the if ~~~ part (Not just returning a boolean value)

def update
  @post = Post.find_by(id: params[:id])
  if @post.save
    redirect_to("/posts/index")
  else
    redirect_to("/posts/#{@post.id}/edit")
  end
end

Display directly in the view without going through the database (render)

Specify render (controller name / view name)

def update
  @post = Post.find_by(id: params[:id])
  if @post.save
    redirect_to("/posts/index")
  else
    render("posts/edit")
  end
end

Image transmission field

Don't forget to specify type

<input name="image" type="file">

Image submission form

Specify {multipart: true}

<%= form_tag("~~~",{multipart: true}) do%>

Save processing of transmitted images

For the received params [: image], use the read method to get the contents of the image data. File.binwrite ("file location", "file contents")

  def update
    @user = User.find_by(id: params[:id])
    @user.name = params[:name]
    @user.email = params[:email]
    @user.image_name = "#{@user.id}.jpg "
    image = params[:image]

    if params[:image]
      File.binwrite("public/user_images/#{@user.image_name}",image.read)
    end
  end

Session settings

Utilize a special variable called session to keep user information even when the page is moved. The value assigned to session is saved in the browser.

session[:user_id] = @user.id

Definition of common variables

application.html.erb is called from all actions. Therefore, it is efficient to define variables to be used in all actions. before_action: By setting the action name, the "action name" is executed before the action is called.

application.rb


before_action :set_current_user

def set_current_user
  @current_user = User.find_by(id: sessino[:user_id])
end

Post table </ font> user_id </ font> Get user information existing in another table

Define an action for the table (Post model in this case) where the data to be used exists

post.rb


def user
  return User.find_by(id: self.user_id)
end

Usage example (Get users associated with posts from posts)

posts_controller.rb


def show
  @post = Post.find_by(id: params[:id])
  @user = @post.user
end

Get post information that exists in another table from id </ font> given to user table </ font>

Define an action for the table (User model in this case) where the data to be used exists

user.rb


def posts
  return Post.where(user_id: self.id)
end

Usage example (Get all posts associated with it from the user and display them)

erb:show.html.erb


<% @user.posts.each do |post|%>
  <img src="<%= "/user_images/#{post.user.image_name}" %>">
  <%= link_to(post.user.name, "/users/#{post.user.id}") %>
  <%= link_to(post.content, "/posts/#{post.id}") %>
<% end%>

List of posts liked by each user

Routing specification

routes.rb


get "users/:id/likes" => "users#likes"

Action definition

rb:users.controller.rb


def likes
  @user = User.find_by(id: params[:id])
  @likes = Like.where(user_id: @user.id)
end

Notation in view

erb:likes.html.erb


<% @likes.each do |like|%>
  <% post = Post.find_by(id: like.post_id)%>
  <img src="<%= "/user_images/#{post.user.image_name}" %>">
  <%= link_to(post.user.name, "/users/#{post.user.id}") %>
  <%= link_to(post.content, "/posts/#{post.id}") %>
<% end%>

Don't forget to use pots_id in the Like table in the second row to get the corresponding post.


Recommended Posts