Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails中设置like按钮_Ruby On Rails_Ruby_Ajax_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 如何在rails中设置like按钮

Ruby on rails 如何在rails中设置like按钮,ruby-on-rails,ruby,ajax,ruby-on-rails-5,Ruby On Rails,Ruby,Ajax,Ruby On Rails 5,你好,我有一个锻炼应用程序,用户应该能够喜欢一些产品。 我可以找到一种展示他喜欢的产品的方法,但我真的不知道如何创建和使用“喜欢”按钮。 我没有使用任何宝石,我不知道如何从头开始 以下是我的模型: class User < ApplicationRecord has_many :likes has_many :liked_products, through: :likes, source: :product end class Product < ApplicationRe

你好,我有一个锻炼应用程序,用户应该能够喜欢一些产品。 我可以找到一种展示他喜欢的产品的方法,但我真的不知道如何创建和使用“喜欢”按钮。 我没有使用任何宝石,我不知道如何从头开始

以下是我的模型:

class User < ApplicationRecord
  has_many :likes
  has_many :liked_products, through: :likes, source: :product
end

class Product < ApplicationRecord
  has_many :likes
end

class Like < ApplicationRecord
  belongs_to :user
  belongs_to :product
end
那是我的产品控制器,我想这里一定有东西,但我不知道怎么做

class ProductsController < ApplicationController

  before_action :find_product, only: :show

  def index
    @products = Product.all
  end

  def show
    #@product.like  => gives an error 404
  end

  private

  def find_product
    @product = Product.find(params[:id])
  end
end
class ProductsController给出一个错误404
结束
私有的
def find_产品
@product=product.find(参数[:id])
结束
结束
我已经创建了一个喜欢的控制器,但它似乎没有用。。。。所以我放弃了那里

class LikesController < ApplicationController

  def new
    @like = Like.new(like_params)
  end

  def create
    @like = Like.new(like_params)
  end

  private

  def like_params
    params.require(:likes).permit(:user_id, :product_id)
  end

end
class-LikesController

我真的很想了解一下这一点:)

你可以尝试以下几点:

路线:

  Rails.application.routes.draw do
    root to: 'visitors#index'
    devise_for :users

    resources :users do 
      resources :products do 
        resources :likes
      end
    end

    resources :products do
      resource :likes
    end
  end
这会给你一些类似的东西:

  ... other routes ...

      user_product_likes GET    /users/:user_id/products/:product_id/likes(.:format)           likes#index
                         POST   /users/:user_id/products/:product_id/likes(.:format)           likes#create
   new_user_product_like GET    /users/:user_id/products/:product_id/likes/new(.:format)       likes#new
  edit_user_product_like GET    /users/:user_id/products/:product_id/likes/:id/edit(.:format)  likes#edit
       user_product_like GET    /users/:user_id/products/:product_id/likes/:id(.:format)       likes#show
                         PATCH  /users/:user_id/products/:product_id/likes/:id(.:format)       likes#update
                         PUT    /users/:user_id/products/:product_id/likes/:id(.:format)       likes#update
                         DELETE /users/:user_id/products/:product_id/likes/:id(.:format)       likes#destroy

  ... other routes ...
然后:

  <%= link_to "Like", user_product_likes_path(@user, @product), method: :post, remote: true %>

按照您的喜好,控制器:

  class LikesController < ApplicationController

    def new
      @like = Like.new(like_params)
    end

    def create
      @like = Like.new(like_params)
      if @like.save
        ... do something happy
      else
        ... do something sad
      end
    end

    private

    def like_params
      params.require(:likes).permit(:user_id, :product_id)
    end

  end
class-LikesController

未经测试,所以买家要小心。你可能需要摆弄你的情妇和其他东西

终于找到了如何设置控制器

class LikesController < ApplicationController

  def create
    @user = current_user.id
    @product = params[:product_id]
    likes = {user_id: @user, product_id: @product}
    @like = Like.new(likes)

    @like.save!
    if @like.save
      redirect_to user_path(@user)
    else
     redirect_to product_path
    end
  end


end

当然可以,很乐意帮忙。如果你愿意,请接受。
  class LikesController < ApplicationController

    def new
      @like = Like.new(like_params)
    end

    def create
      @like = Like.new(like_params)
      if @like.save
        ... do something happy
      else
        ... do something sad
      end
    end

    private

    def like_params
      params.require(:likes).permit(:user_id, :product_id)
    end

  end
class LikesController < ApplicationController

  def create
    @user = current_user.id
    @product = params[:product_id]
    likes = {user_id: @user, product_id: @product}
    @like = Like.new(likes)

    @like.save!
    if @like.save
      redirect_to user_path(@user)
    else
     redirect_to product_path
    end
  end


end
<%= link_to "Like", product_likes_path(@product), method: :post %>
Rails.application.routes.draw do
    root to: 'products#index'
    devise_for :users

    resources :users

    resources :users do
      resources :products do
        resources :likes
      end
    end
  end