Home>
I'm in the process of making a Twitter clone.
I would like to install a "Add/Delete Favorites" button, but I asked a question because an error occurred when I created a partial file and tried to install the button.
As a result of investigation, it seems that there is no file for favorites/_favorite_button, but I stumbled because I did not understand why this error occurred even though there was a file.
Code where the error is occurringviews/micropost/_microposts.html.erb
<ul><% microposts.each do | micropost |%><li> " alt = ""> <% = link_to micropost.user.name, user_path (micropost.user)%><span>posted at<% = micropost.created_at%></span> <% if current_user == micropost.user%> <% = link_to "Delete", micropost, method:: delete, data: {confirm: "You sure?"},
class:'btn btn-danger btn-sm'%> <% end%> <% = render'favorites/favorite_button', micropost: @micropost%> <p><% = micropost.content%></p> </li><% end%><% = paginate microposts%></ul>
Source code written when adding "favorite function"
db/migrate/20201012042419_create_favorites.rb
class CreateFavorites</pre>
<p>models/favorite.rb</p>
<pre><code>class Favorite</pre>
<p>user.rb</p>
<pre><code>class User #Favorite function
def favorite (micropost)
self.favorites.find_or_create_by (micropost_id: micropost.id)
end
def unfavorite (micropost)
favorite = self.favorites.find_by (micropost_id: micropost.id)
favorite.destroy if favorite
end
def likes? (micropost)
self.likes.include? (micropost)
end
end
config/routes.rb
Rails.application.routes.draw do
root to:'toppages # index'
get'login', to:'sessions # new'
post'login', to:'sessions # create'
delete'logout', to:'sessions # destroy'get'signup', to:'users # new'
resources: users, only: [: index,: show,: new,: create] do
member do
get: followings
get: followers
get: likes
end
end
resources: microposts, only: [: create,: destroy]
resources: relationships, only: [: create,: destroy]
resources: favorites, only: [: create,: destroy]
end
controller/favorites_controller.rb
class FavoritesController</pre>
<p>views/favorites/_favorite_button_html.erb</p>
<pre><code><% if current_user.likes? (Micropost)%><% = form_with (model: current_user.favorites.find_by (micropost_id: micropost.id), local: true, method:: delete) do | f |%><% = hidden_field_tag: micropost_id, micropost.id%><% = f.submit'Unfavorite', class:'btn btn-danger btn-block'%><% end%><% else%><% = form_with (model: current_user.favorites.build, local: true) do | f |%><% = hidden_field_tag: micropost_id, micropost.id%><% = f.submit'Favorite', class:'btn btn-primary btn-block'%><% end%><% end%>
Please
If there is any missing source code, I will post it.
-
Answer # 1
Related articles
- ruby on rails - unable to install direnv
- ruby on rails - rails bundle install sassc stops
- ruby - i can't install ffi with everyday rails setup
- ruby on rails - i want the login button to look like the sign-up button in the image
- ruby on rails - even if i fill in the message form and press the send button, it is not reflected in the view
- ruby - i want to know where to install rails gem and how to manage it
- ruby on rails 5 - bundle install stops at sassc
- i get an error when i try to install mysql with bundle install [ruby on rails]
- [sqlite, ruby, rails] i want to sort the hash value data obtained from the table as i expected
- ruby on rails - about the legitimacy of db design
- ruby on rails - i want to solve the problem that scss is not applied after modifying the scss file with rails
- ruby on rails - not created
- ruby on rails - i want to sort the search results
- ruby on rails - i get an error in a program that changes over time
- ruby - [rails] how to resolve activerecord errors
- about validation of ruby on rails
- ruby - allow rails chat app to save to only one room
- ruby on rails 5 - transaction exception handling doesn't work
- ruby - [rails 5] i want to send an email to a specific user using action mailer
Related questions
- ruby - sass :: syntaxerror on rails s
- javascript - i want the scroll bar to always appear at the bottom of the chat screen
- ruby - i want to use two rails renders
- ruby - the top screen is displayed at http: // localhost: 3000/users/sign_in
- ruby - [rails] the class attribute specified by the div tag enclosed in simple_format is not applied
- ruby on rails removal function
- ruby on rails sorting
- html - i can't set the regular expression well
- ruby - rails i want to make the created_at time the same when creating multiple data at once
- ruby - dynamic form implementation in nested_form
I recreated the file again and it was done!
There may have been a half-width space or something.
If you can't rename it and check the spelling, it may be faster to recreate it.