如何改善代码以在远程按钮之间切换状态?

时间:2014-10-26 16:04:24

标签: ruby-on-rails ruby-on-rails-4

我有模特用户,收藏夹,小孩和妈妈。

class User < ActiveRecord::Base
  has_many :kids
  has_many :favorites

  def favorited?(mom)
    favorites.where(mom_id: mom.id ).any?
  end
end

class Mom < ActiveRecord::Base
  has_many :kids
  has_many :favorites
end

class Kid < ActiveRecord::Base
  belongs_to :user
  belongs_to :mom
end

class Favorite < ActiveRecord::Base
  belongs_to :user
  belongs_to :mom
end

我为用户列出了一堆孩子,并允许用户在孩子的列表中收藏喜欢的妈妈们。

class KidsController < ApplicationController
  before_action :authenticate_user!

  index
    @kids = Kid.paginate(:page => params[:page])
  end
end

class FavoritesController < ApplicationController
  before_action :authenticate_user!
  respond_to :js, :json, :html

  def create
    @mom = Mom.find(params[:id])
    @favorite = current_user.favorites.build(params[:favorites])
    @favorite.mom_id = @mom.id
    @favorite.save

    if request.xhr?
      head :ok
    else
      redirect_to :back
    end
  end

  def destroy
    @favorite = current_user.favorites.find(params[:id])
    @favorite.destroy
  end
end

孩子/索引

- @kids.each do |kid|
  - if current_user.favorited?(kid.mom)
    %li
      = link_to({ controller: "favorites", action: "destroy", id: current_user.favorites.find_by(mom_id: kid.mom.id).id }, { method: :delete,  remote: true }, data: { toggle_text: 'Favorite', toggle_href: { controller: 'favorites', action: 'create', id: kid.mom.id }}) do
        %i.fa.fa-star-o.fa-fw
        Unfavorite
  - else
    %li
      = link_to({ controller: 'favorites', action: 'create', id: kid.mom.id }, { method: :post,  remote: true }, data: { toggle_text: 'Unfavorite', toggle_href: { controller: "favorites", action: "destroy", id: current_user.favorites.find_by(mom_id: kid.mom.id) } }) do
        %i.fa.fa-star.fa-fw
        Favorite

我无法正常使用此功能,因为我的链接设置方式,我猜不到它。或者它可能是别的东西。我不知道替代方法,所以我在这里寻求帮助。我需要在代码中进行哪些更改才能切换链接的不同状态,并通过Ajax使其保持不受欢迎和收藏?

1 个答案:

答案 0 :(得分:1)

如果你想切换链接,那么首先你需要一些东西来定位这个特定的链接:

- @kids.each do |kid|
  %li{id: "kid_link_#{kid.mom.id}"}
    - if current_user.favorited?(kid.mom)
      = render partial: "favorite" 
    - else
      = render partial: "unfavorite"

#_unfavorite.html.erb
= link_to({ controller: "favorites", action: "create"}, method: :delete,  remote: true ) do
  %i.fa.fa-star-o.fa-fw
    Unfavorite

#_favorite.html.erb
= link_to({ controller: 'favorites', action: 'destroy'}, method: :post,  remote: true ) do
  %i.fa.fa-star.fa-fw
    Favorite

现在你需要在命中控制器代码时处理ajax请求

def create
  @mom = Mom.find(params[:id])
  @favorite = current_user.favorites.build(favorite_params)
  @favorite.mom_id = @mom.id
  @favorite.save
end

def destroy
  @mom = Mom.find(params[:id]) # some logic to find mom so that you could target its link
  @favorite = current_user.favorites.find(params[:id])
  @favorite.destroy
end

private
def favorite_params
  params.require(:favorites).permit(:favorite_form_params)
end

#create.js.erb
$("#kid_link_<%=j @mom.id %>").html("<%=j render partial: "favorite" %>");

#destroy.js.erb
$("#kid_link_<%=j @mom.id %>").html("<%=j render partial: "unfavorite" %>");