SASS为edit.html.haml
.editingback
position: absolute
width: 100%
height: 102%
background: rgba(0, 0, 0, 0.3)
z-index: 500
display: none
.editing
z-index: 501
width: 300px
height: 150px
background: #eee
border: 1px solid #adadad
border-radius: 3px
position: absolute
top: 40%
left: 50%
transform: translate(-50%, -50%)
box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5)
font-family: $la
padding: 15px
text-align: center
font-size: 13px
display: none
#editing_button
border: 1px solid #eee
background: #383838
color: white
padding: 7px
edit.html.haml
.editingback
.editing
= @post.state
is editing this post at the moment, please try again later!
%br
%br
= link_to 'OK', posts_path, id: 'editing_button'
= form_for (@post) do |f|
= f.text_field :title
= f.text_area :content
= f.submit 'Create'
:javascript
var pageid = '<%= @post.id %>';
var statevalue = '<%= @post.state %>';
var currentuser = '<%= current_user.email %>';
window.onload = checkstate();
function checkstate() {
if (statevalue == "") { // If statevalue is equal to "" it will send a ajax request to the updatestate action with the posts id. //
$.ajax({
url : '/posts/changestate/' + pageid,
type : 'POST',
data : $('#changenotestate').serialize(),
});
window.onbeforeunload = removestate; // When the user leave the site will it run the removestate function to remove state //
} else if (statevalue != currentuser) { // If a new user enter the page after you and the statevalue not is equal to the currentuser, show the warning //
$(".editingback").show();
$(".editing").fadeIn(2000);
} else {
}
};
// A function there will send a ajax request to the removestate action //
function removestate(){
$.ajax({
url : '/posts/removestate/' + pageid,
type : 'POST',
data : $('#changenotestate').serialize(),
});
};
posts_controller.rb
def edit ## Edit post action
@post = Post.find(params[:id])
end
def update ## Update post action
@post = Post.find(params[:id])
@post.update(post_params)
if @post.save
redirect_to posts_path
else
render 'edit'
end
end
def updatestate ## Action there will add current_user.email to the state field
@post = Post.find(params[:id])
@post.update(:state => current_user.email)
redirect_to :back
end
def removestate ## Action there will remove current_user.email to the state field
@post = Post.find(params[:id])
@post.update(:state => '')
redirect_to :back
end
的routes.rb
resources :posts # RESTFULL ROUTES
post '/posts/changestate/:id' => 'posts', :action => 'updatestate' #ROUTE TO UPDATE POST STATE
post '/posts/removestate/:id' => 'posts', :action => 'removestate' # ROUTE TO REMOVE POST STATE
帖子表有以下字段:
标题,内容,状态。
我真的很喜欢这个解决方案,但所有它实际上做的是提醒其他用户已在编辑他要编辑的帖子的用户。
因此,用户可以使用他的浏览器“控件元素”功能来删除警告。 因此,我会研究“悲观锁定”以使其安全。
享受..请告诉我,如果你知道更好的方法。
听起来很有趣。我会毫不犹豫地阅读。感谢您的链接。但我可能很快就会有另一种解决方案。如果用户全部准备好在页面上,将会警告新用户。 – niiicolai 2014-10-19 19:45:49