如何在rails中设置类似按钮

时间:2017-05-05 17:10:42

标签: ruby-on-rails ruby ajax ruby-on-rails-5

您好我有一个运动应用程序,用户应该能够喜欢某些产品。 我可以找到一种方法来展示他喜欢的产品,但我真的无法想象如何创建和工作 like button 。 我没有使用任何宝石,我不想从Scratch了解如何做到这一点。

以下是我的模特:

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

在我看来,产品展示了我想要的按钮:

<h1><%= @product.name %></h1>

<%= link_to "Like", product_likes_path(@product), method: :put, remote: true %>

我的路线:

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

  resources :products do
    resource :likes
  end
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 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

我真的很喜欢这个... :)

2 个答案:

答案 0 :(得分:2)

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

// start cam
 void Start () {
     devices = WebCamTexture.devices;
     background = GetComponent<RawImage> ();
     devCam = new WebCamTexture ();
     background.texture = devCam;
     devCam.deviceName = devices [0].name;
     devCam.Play ();
 }

 void OnGUI()
 {
     GUI.skin = skin;   
        //swap front and back camera
     if (GUI.Button (new Rect ((Screen.width / 2) - 1200, Screen.height - 650, 250, 250),"", GUI.skin.GetStyle("btn1"))) {
         devCam.Stop();
         devCam.deviceName = (devCam.deviceName == devices[0].name) ? devices[1].name : devices[0].name;
         devCam.Play();
     }
         //snap picture
     if (GUI.Button (new Rect ((Screen.width / 2) - 1200, Screen.height - 350, 250, 250), "", GUI.skin.GetStyle ("btn2"))) {
         OnSelectCapture ();
                //freeze cam here?
     }
 }


 public void OnSelectCapture()
 {
     imgID++;
     string fileName = imgID.ToString () + ".png";
     Texture2D snap = new Texture2D (devCam.width, devCam.height); 
     Color[] c;                                                         
     c = devCam.GetPixels ();                             
     snap.SetPixels (c);                                                 
     snap.Apply ();                                                    

     // Save created Texture2D (snap) into disk as .png
     System.IO.File.WriteAllBytes (Application.persistentDataPath +"/"+ fileName, snap.EncodeToPNG ());
 }

buttton

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 %>

答案 1 :(得分:1)

你可以尝试这些方法:

路线:

  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 %>

在你的LikesController中:

  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

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

相关问题