[Ruby] Looking back on Progate Ruby on Rails 5

2 minute read

Progate Ruby on Rails Course impression

I spent about 200 hours learning programming and embarked on a Rails course. Although the learning so far has proceeded smoothly, this time it was difficult. I do not fully understand the content of Dojo Course 4. Make a note of it so 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 working with tables ―― Create model

rails g model Post content:text

rails g model model name column name: data type *Specify the model name in the singular system. ―― Creating a migration file

rails g migration add_image_name

Get value from URL

Specify hash in routing.

routes.rb


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

Can be obtained by setting params [hash]

controller.rb


def show
  @id = params[:id]
end

*Values that can be obtained with params[:~~] are character strings, so format them when performing comparisons.

Setting the form destination

erb:posts/new.html.erb


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

don’t forget do

Link description method at #post Specify the method post as the third argument.

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

Branch in save process to database

Note that the save process is executed in the if ~~~ part (Not only returns boolean)

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 the type

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

Image submission form

Specify {multipart: true}

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

Save processing of sent image

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 the user information even when the page is moved. The value assigned to session is saved in the browser.

session[:user_id] = @user.id

Define common variables

application.html.erb is called from all actions. Therefore, it is efficient to define variables that are 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 has user_id Get user information in another table Define an action for the table (here, Post model) where the data to be used exists

post.rb


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

Example of use (get the user associated with it from the post)

posts_controller.rb


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

#User table has an id Gets post information existing in another table Define an action for the table (here, User model) that contains the data to be used

user.rb


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

Example of use (obtain and display all posts associated with it from the user)

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

Specifying routing

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 the part that gets the corresponding post using pots_id in the Like table on the second line.