喂!我正在教自己Ruby,并且已经坚持了几天:
我目前正在使用MooTools-1.3-compat和Rails 3。
我想在有人点击它时立即将一个按钮(称为“Follow”)替换为另一个按钮(称为“Unfollow”)。我正在使用:remote =>是并且有一个以 .js.erb 结尾的文件被调用...我只是需要帮助找出这个.js文件中的内容
“关注”按钮位于id =“follow_form”的div中,但页面上有许多按钮,它们都有一个id =“follow_form”...即。 $(“follow_form”)。set(...)替换第一个元素,这是不正确的。我需要帮助替换拨打电话的按钮。
我看了this tutorial,但下面这行对我不起作用。可能是因为我使用的是MooTools而不是Prototype?
$(“follow_form”)。update(“<%= escape_javascript(render('users / unfollow'))%>”)
PS。这就是我到目前为止所做的工作:
app / views / shared中的:
<%= form_for current_user.subscriptions.build(:event => @event), :remote => true do |f| %>
<div><%= f.hidden_field :event %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
app / views / events / create.js.erb 中的
*在app / controllers / subscriptions_controller.rb * 非常感谢任何帮助!alert("follow!"); //Temporary...this is what I'm trying to replace
def create
@subscription = current_user.subscriptions.build(params[:subscription])
@subscription.save
respond_to do |format|
format.html { redirect_to(..) }
format.js {render :layout}
end
答案 0 :(得分:0)
在Mootools中,替换Follow with Unfollow的代码如下所示。我并不完全理解您的问题,但您需要为按钮指定一个ID。
document.addEvent('domready',function(){
$$('follow_form').addEvent('click',function(e){
//Ajax Here
this.set('value','Unfollow');
});
});
如果您不能/无法为每个表单元素添加唯一ID,则此代码也适用于您:
$$('input').addEvent('click',function(){
if(this.value == 'Follow') {
//Ajax Here
this.set('value','Unfollow');
}
});