我有困难的时候试图让我的部分刷新按钮preSS。我想简单地刷新分区,而不是整个页面。下面是我的看法:
< DIV ID =AJAX>
<%=渲染布局/逼供%>
< / DIV>
然后在我的部分 _confessions.html.erb
,我有一些基本的HTML和两个按钮类似于此:
<%=的form_tag({:控制器=>:招供,:动作=>:upvote,:ID => conf.id},{:方法=&GT ;:放})办%GT;
<%= submit_tag'像'%>
<%结束%GT;
我的confessions_controller.rb:
高清upvote
@confession = Confession.find(PARAMS [:ID])
Confession.increment_counter:upvotes,@confession
respond_to代码做|格式|
format.js
结束
结束
最后, upvote.js.erb
:
$('#阿贾克斯)HTML(<%= escape_javascript(渲染(:部分=>'招供'))html_safe%>中);
提交到我的数据库的操作工作,但页面现在重定向到 / upvote?ID = 9
(ID可以不同),而不是只刷新股利。我究竟做错了什么?我是新来的Rails,所以我可能失去了一些东西完全明显...
编辑:这是我的文件夹结构:
我的看法:意见/页/ home.html.erb
我的部分:意见/布局/ _confessions.html.erb
我的控制器:控制器/ confessions_controller.rb
我的js.erb文件:意见/口供/ upvote.js.erb
在耙路线
供词GET /confessions(.:format)告白#指数
POST /confessions(.:format)告白#创建
new_confession GET /confessions/new(.:format)告白#新
edit_confession GET /confessions/:id/edit(.:format)告白#编辑
忏悔GET /confessions/:id(.:format)告白#秀
PUT /confessions/:id(.:format)告白#更新
DELETE /confessions/:id(.:format)告白#销毁
upvote /upvote(.:format)告白#upvote
downvote /downvote(.:format)告白#downvote
根/页#回家
解决方案
请确保你有这样的你的application.js
// =需要的jQuery
// =需要jquery_ujs
供参考:Rails的form_for:远程=>真不调用JS方法
然后,改变这种情况,
$('#阿贾克斯)HTML(<%= escape_javascript(渲染(:部分=>'招供'))html_safe%>中);
要:
$('#阿贾克斯)HTML(<%= escape_javascript(渲染(:部分=>布局/供词))html_safe%>中) ;
I am having a difficult time trying to get my partial to refresh on button press. I want to simply refresh the div, and not the whole page. Here is what I have in the view:
<div id="ajax">
<%= render 'layouts/confessions' %>
</div>
Then in my partial _confessions.html.erb
, I have some basic html and two buttons similar to this:
<%= form_tag( { :controller => :confessions, :action => :upvote, :id => conf.id }, { :method => :put } ) do %>
<%= submit_tag 'Like' %>
<% end %>
My confessions_controller.rb:
def upvote
@confession = Confession.find(params[:id])
Confession.increment_counter :upvotes, @confession
respond_to do |format|
format.js
end
end
And finally, upvote.js.erb
:
$('#ajax').html("<%= escape_javascript(render(:partial => 'confessions')).html_safe %>");
The action of submitting to my database is working, but the page is now redirecting to /upvote?id=9
(id can be different), instead of just refreshing the div. What am I doing wrong? I am new to Rails, so I could be missing something completely obvious...
EDIT: Here is my folder structure:
My view: views/pages/home.html.erb
My partial: views/layouts/_confessions.html.erb
My Controller: controllers/confessions_controller.rb
My js.erb file: views/confessions/upvote.js.erb
After rake routes
confessions GET /confessions(.:format) confessions#index
POST /confessions(.:format) confessions#create
new_confession GET /confessions/new(.:format) confessions#new
edit_confession GET /confessions/:id/edit(.:format) confessions#edit
confession GET /confessions/:id(.:format) confessions#show
PUT /confessions/:id(.:format) confessions#update
DELETE /confessions/:id(.:format) confessions#destroy
upvote /upvote(.:format) confessions#upvote
downvote /downvote(.:format) confessions#downvote
root / pages#home
解决方案
Make sure you have this in your application.js
//= require jquery
//= require jquery_ujs
FYI: Rails form_for :remote=>true is not calling js method
Then, change this,
$('#ajax').html("<%= escape_javascript(render(:partial => 'confessions')).html_safe %>");
To:
$('#ajax').html("<%= escape_javascript(render(:partial => 'layouts/confessions')).html_safe %>");
上一篇:阿贾克斯字preSS传递数据数据、阿贾克斯字、preSS
下一篇:从运行Javascript和jQuery Mobile的用户界面的Python CGI脚本用户界面、脚本、jQuery、Javascript