由于我启用了remote true,它应该立即将切换按钮“Follow”更改为“Un-Follow”。
但事实并非如此。如果我重新加载页面,切换按钮会显示更改的标签。
所以这意味着它实际上是在改变背景中跟随列的值。
我已经为此花了 3 天时间,但我仍然不知道:(
谁能帮我解决这个问题?代码有什么问题吗?还是我缺少一些 JQuery 文件?我是 Rails 的新手。 ..
宝石文件
...
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
gem 'jquery-ui-rails'
end
gem 'jquery-rails'
...
资产/javascripts/application.js
...
//= require twitter/bootstrap
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree
...
意见/共享/_follow_user.html.erb
<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= link_to sanitize('<i class="icon-remove icon-white"></i> ') + 'Un-Follow', user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, :class => 'btn' %>
<% else %>
<%= link_to sanitize('<i class="icon-ok icon-white"></i> ') + 'Follow', user_follows_path(user.to_param), :remote => true, :class => 'btn btn-primary' %>
<% end %>
<% end %>
意见/用户/create.js.erb
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
意见/用户/destroy.js.erb
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
控制器/follows_controller.rb
class FollowsController < ApplicationController
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
配置/路由.rb
...
resources :users, :only => [:index, :show] do
resources :follows, :only => [:create, :destroy]
end
...
模型/用户.rb
class User < ActiveRecord::Base
...
acts_as_follower
acts_as_followable
...
end
意见/用户/show.html.erb
<h1>Users#show</h1>
<p id="notice"><%= notice %></p>
<p>
<b>User:</b>
<%= @user.user_profile.user_id %>
</p>
<p>
<b>Language:</b>
<%= @user.user_profile.language_id %>
</p>
<p>
<b>Country:</b>
<%= @user.user_profile.country_id %>
</p>
<p>
<b>Prefecture:</b>
<%= @user.user_profile.prefecture_id %>
</p>
<p>
<b>Gender:</b>
<%= @user.user_profile.gender_id %>
</p>
<p>
<b>Nickname:</b>
<%= @user.user_profile.nickname %>
</p>
<p>
<b>Introduction:</b>
<%= @user.user_profile.introduction %>
</p>
<p>
<b>Picture url:</b>
<%= @user.user_profile.picture_url %>
</p>
<% if user_signed_in? %>
<div id="follow_user">
<%= render :partial => "shared/follow_user", :locals => {:user => @user} %>
</div>
<% end %>