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.
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
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
erb:posts/new.html.erb
<%= form_tag("/posts/create") do%>
<% end %>
Don't forget do
Specify the method post as the third argument.
<%= link_to("delete","/posts/#{@post.id}/destroy",{method: "post"})%>
<% end %>
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
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
Don't forget to specify type
<input name="image" type="file">
Specify {multipart: true}
<%= form_tag("~~~",{multipart: true}) do%>
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
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
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
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
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%>
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