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
стол переговоров имеет следующие поля:
Id, Title, Content, State.
Я очень люблю этого решения, но все это на самом деле сделать это, чтобы предупредить пользователя о том, что другой пользователь уже редактируемые должность он хотел изменить.
Таким образом, пользователь может просто использовать функцию «элемент управления» браузера, чтобы удалить предупреждение. Поэтому я рассмотрю «пессимистическую блокировку», чтобы сделать ее безопасной.
Наслаждайтесь .. и, пожалуйста, скажите мне, если вы знаете лучший способ.
Этот звук интересный. Я буду демонстративно читать об этом. Спасибо за ссылку. Но скоро у меня будет другое решение. Там будет предупреждать нового пользователя, если пользователь все готов на странице. – niiicolai