【发布时间】:2014-11-05 23:09:19
【问题描述】:
我不确定这里的问题是什么,不知道我到底在问什么,但我会尽力而为。 我试图在我认为问题所在的地方加粗以使其更清楚。
我是第一次使用 ajax。在应用程序中,用户 (1) 搜索用户 (2),找到用户 (2) 后,用户 (1) 可以单击“添加关系”按钮,该按钮向用户 (2) 发送关系请求。 “添加关系”按钮应立即更改为“请求的关系”,然后在刷新页面后,该按钮应更改为“编辑关系”。
'添加关系' = 尚不存在关系 'relationship requested' = 存在待处理的关系,就像一条成功的闪光信息 '编辑关系' = 存在待处理关系或存在已接受关系
除了最后一部分 刷新浏览器页面时按钮应更改为“编辑关系”之外,一切正常。而是出现“添加关系”
这个“编辑关系”按钮不会出现的任何想法?我正在使用状态机、draper 和 js-routes gem。
查看/用户/索引:
搜索用户 (2) 时,他们的姓名出现在哪里,并且“添加关系”、“编辑关系”或“请求的关系”按钮出现在名称旁边
我认为这里的 if 语句是行不通的。为什么它不根据它是挂起还是接受来查找关系?
<% if logged_in? %>
<ul>
<% @users.each do |user| %>
<li>
<%= user.name %>
<div id="relationship-status">
<% if current_user.following.include?(user.id) || current_user.pending_following.include?(user.id) %>
<%= link_to "Edit Relationship", edit_relationship_path(followed_id: user.id), class: "btn btn-primary" %>
<% else %>
<%= link_to "Add Relationship", new_relationship_path(followed_id: user.id), class: "btn btn-primary", id: 'add-relationship', data: { followed_id: user.id.to_param } %>
<% end %>
</div>
</li>
<% end %>
</ul>
<% end %>
控制者/用户:
def index
@users = User.search(params[:search])
end
relationship.js:
$(document).ready(function() {
$('#add-relationship').click(function(event) {
event.preventDefault();
var addRelationshipBtn = $(this);
$.ajax({
url: Routes.relationships_path({relationship: { followed_id: addRelationshipBtn.data('followedId') }}),
dataType: 'json',
type: 'POST',
success: function(e) {
addRelationshipBtn.hide();
$('#relationship-status').html("<a href='#' class='btn btn-success'>Relationship Requested</a>");
}
});
});
});
型号/用户:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :pending_relationships, class_name: "Relationship",
foreign_key: "follower_id"
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, -> { where(relationships: { state: "accepted" } ) }, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
has_many :pending_following, -> { where(relationships: { state: "pending" } ) }, through: :pending_relationships, source: :followed
关系/装饰者:
class RelationshipDecorator < Draper::Decorator
delegate_all
decorates :relationship
def relationship_state
model.state.titleize
end
def sub_message
case model.state
when 'pending'
"Relationship request pending"
when 'accepted'
"You are now connected with #{model.followed.name}"
end
end
end
编辑:
db/迁移:
class AddStateToRelationships < ActiveRecord::Migration
def change
add_column :relationships, :state, :string
add_index :relationships, :state
end
end
模型/关系:
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
after_destroy :delete_mutual_relationship!
state_machine :state, initial: :pending do
after_transition on: :accept, do: [:send_acceptance_email, :accept_mutual_relationship!]
state :requested
event :accept do
transition any => :accepted
end
end
终端输出:
Started POST "/relationships?relationship%5Bfollowed_id%5D=25" for 127.0.0.1 at 2014-11-06 16:35:26 +1100
Processing by RelationshipsController#create as JSON
Parameters: {"relationship"=>{"followed_id"=>"25"}}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 25]]
(0.1ms) begin transaction
SQL (5.4ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "state", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", "2014-11-06 05:35:26.360104"], ["followed_id", 25], ["follower_id", 1], ["state", "pending"], ["updated_at", "2014-11-06 05:35:26.360104"]]
SQL (0.2ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "state", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", "2014-11-06 05:35:26.368921"], ["followed_id", 1], ["follower_id", 25], ["state", "requested"], ["updated_at", "2014-11-06 05:35:26.368921"]]
Relationship Load (0.1ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."id" = ? LIMIT 1 [["id", 49]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 25]]
Rendered user_mailer/relationship_requested.html.erb (0.2ms)
UserMailer#relationship_requested: processed outbound mail in 27.9ms
Sent mail to example-24@example.com (14.2ms)
Date: Thu, 06 Nov 2014 16:35:26 +1100
From: noreply@example.com
To: example-24@example.com
Message-ID: <545b089e6206e_6c313ff72cf9cf78434189f@example.local.mail>
Subject: Firstname Surname wants to follow you. Please log in to accept
this request
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hi example-24,
Firstname Surname wants to follow you.
(7.7ms) commit transaction
Completed 200 OK in 71ms (Views: 0.3ms | ActiveRecord: 14.4ms)
当我(用户 1)搜索用户 example-24(用户 id = 25)然后按“添加关系”按钮时会发生什么(如 sqlitebrowser 所示:(参见图像底部的 2 行)与本例相关)
编辑:
用户/控制器:
def create
if params[:relationship] && params[:relationship].has_key?(:followed_id)
@followed = User.find(params[:relationship][:followed_id])
# @followed = User.where(name: params[:relationship][:followed_id]).first
@relationship = Relationship.request(current_user, @followed)
respond_to do |format|
if @relationship.new_record?
format.html do
flash[:danger] = "There was a problem creating that relationship request"
redirect_to followed_path(@followed)
end
format.json { render json: @relationship.to_json, status: :precondition_failed }
else
format.html do
flash[:success] = "Friend request sent"
redirect_to followed_path(@followed)
end
format.json { render json: @relationship.to_json }
end
end
else
flash[:danger] = "Friend Required"
redirect_to users_path
end
end
【问题讨论】:
-
您是否从 Rails 控制台尝试过?我们看不到您的状态机代码,或者您在数据库中更改状态或某些值的某些代码。 dataType 是 json,我认为可能是“脚本”。
-
感谢您回复@argentum47!如何从 Rails 控制台尝试?我不确定'dataType 是 json,我认为可能是'脚本'。'是什么意思?我对此很陌生。不过,我现在已经为状态机提供了代码(模型和数据库迁移)
-
我还刚刚添加了终端输出,显示按下“添加关系”按钮时会发生什么。它似乎确实被处理为 JSON。如果我使用 sqlitebrowser,我可以看到在按下“添加关系”按钮后创建了 2 个关系。我现在也会在问题中添加一张图片,以便您查看
-
你正在发送一个 ajax 请求,所以你的数据类型应该是 'script' 而不是 'json' 在你的 javascript 中。
-
@argentum47 我改变了它,'关系请求按钮不会再出现,终端说它正在被处理为 html,我得到'SQLite3 :: BusyException:数据库被锁定:......'我将它改回 json,但仍然收到“SQLite3::BusyException:数据库已锁定:...”。现在我更迷茫了
标签: javascript ruby-on-rails ajax variables draper