Marco De Rossi
Joined 1/25/2021
Marco De Rossi said over 3 years ago on Reactive Applications with Stimulus Reflex :
hello   i need some help . how do i set a like button.

class Business < ApplicationRecord
.........
  belongs_to :user 
  has_many :likes

class Like < ApplicationRecord
belongs_to :user  
belongs_to :business

end



class User < ApplicationRecord
has_many :likes

 
business/show.html.erb
<%= button_tag "Like", data: {reflex: "click->Likes#like", id: @business.id} %>


class LikesReflex < ApplicationReflex  
def like    
business = Business.find(element.dataset[:id])    
business.likes.find_or_create_by(user: current_user)  

......

i need help to set the likeReflex controller .
i want login users to like business and add them to thier user.likes

Marco De Rossi said over 3 years ago on Form Wizards from Scratch :
Great tips. 
Can you can a previous and next step buttons?. If I have form with 8 steps. Let assume I am on the 7th step and decide to change an attribute on step 2. How do I get back to step 2?
Thanks

Marco De Rossi said over 3 years ago on Like Relationships and Global ID :
hi   
How do I scope for posts liked by a user and show them on the user's profile page?

I tried 
user.users_posts

user.liked_posts,
 
 none of them gives me the results
user.users_posts get only the id on the posts 


Marco De Rossi said over 3 years ago on Like Relationships and Global ID :
  Thanks, I had a typo.  I have loop all users liked posts on the user profile page.  I get this error when I add the unfollow link
undefined method `to_sgid

## user_profile.html.erb
@liked_post.each do |post|%>

link_to  "Unfollow",posts_like_path(post, id: @liked_post.to_sgid(expires_in: 1.hour)),method: :delele

## users_controller

def user_profile
  @user= current_user
  @posts = @user.posts.all 
  @liked_post = @user.liked_post   
 end

This setup works fine on the post-show page but not on the user profile page

Marco De Rossi said over 3 years ago on Like Relationships and Global ID :

link_to  "Unfollow",post_like_path(post, id: post.to_sgid(expires_in: 1.hour)),method: :delete
Error referencing Posts::LikesController#destroy
PG::ForeignKeyViolation: ERROR: update or delete on table "posts" violates foreign key constraint "fk_rails_fbe88b52fa" on table "users_posts"
DETAIL: Key (id)=(10) is still referenced from table "users_posts".

def destroy    
  post = Post.friendly.find(params[:post_id])
  user_post = GlobalID::Locator.locate_signed(params[:id])
  user_post.destroy!    
  redirect_to post_path(post), alert: "unfollow"  
end