我的笔记应用程序使用ajax在用户输入时保存对笔记的更改。在编辑笔记时,它的效果非常好。
然而,当我制作一个" New Note"每个更改都保存为新注释。不好。当我应该有一个笔记时,我会得到许多笔记。
有没有办法以某种方式重新路由用户......所以当你点击" New Note"在后台创建一个新笔记...然后立即编辑一个空白笔记?
我意识到我可能会想到这一切都错了。因此,欢迎提出如何更好地解决此问题的建议。
如果您看一下Evernote的网络应用程序 - 我基本上只是模仿该功能 - 虽然设计有点不同。
相关代码:
我的笔记控制器。
class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Note created!"
redirect_to current_user
else
@feed_items = []
render 'static_pages/home'
end
end
def edit
end
def update
respond_to do |format|
if @micropost.update(micropost_params)
format.html { redirect_to current_user, notice: 'Note was successfully updated.' }
format.json { render :show, status: :ok, location: @micropost }
else
format.html { render :edit }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end
def show
end
def index
end
def destroy
@micropost.destroy
if @micropost.destroy
flash[:success] = "Note deleted!"
redirect_to current_user
else
flash[:success] = "Oops! Something went wrong."
render 'static_pages/home'
end
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
我的&#34;表格&#34; - 我正在使用可编辑的内容,然后抓住那个w / JS并将其丢弃在表单中。
<%= form_for(@micropost, remote: true, :html => {:id => "noteform"}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= button_tag( class: "useicons clear save-button the-logo", type: "submit", title: "save", onclick: "return getContent()") do %>
<%= image_tag("upload.svg", alt: "Upload") %>
<span class="verbal saveit clear">Save</span>
<% end %>
<% end %>
JS抓取内容可编辑并将其放在表单字段中,然后使用ajax在3秒暂停后发布。
<script type="text/javascript">
function getContent(){
document.getElementById("micropost_content").value = document.getElementById("haiku-note").innerHTML;
} setInterval( "getContent()", 0 );
</script>
<script type="text/javascript">
function closeWarning(){
return 'Unsaved changes. Give it a second. Then go.'
}
var timer;
var timeout = 3000;
$('#haiku-note').on("input", function() {
clearTimeout(timer);
window.onbeforeunload = closeWarning
if ($('#haiku-note').val) {
timer = setTimeout(function(){
$('#noteform').trigger("submit.rails");
window.onbeforeunload = false;
}, timeout);
}
});
</script>
这是侧边栏的一部分,您可以在其中转到“备注列表”页面和“新建备注”页面。
<div class="dropdown">
<button class="icoround dropdown-toggle sr-only the-logo" type="button" id="dLabel" data-toggle="dropdown">
<span style="font-size:40px;">☰</span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation"><%= link_to "New Note", root_path %></li>
<li role="presentation"><%= link_to "Notes", current_user %></li>
<li role="presentation"><%= link_to "Account", edit_user_path(current_user) %></li>
<li role="presentation"><%= link_to "About", about_path %></li>
<li role="presentation" class="divider"></li>
<li role="presentation"><%= link_to "Sign out", signout_path, method: "delete" %></li>
</ul>
</div>
如果您需要查看更多代码,请告诉我,整个项目还在github
答案 0 :(得分:0)
我更改了我的控制器#create action。因此,当保存Note时,它会自动重定向到Edit that Note。
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
render js: "window.location.pathname = #{edit_micropost_path(@micropost.id).to_json}"
else
end
end