Javascript 第一次尝试在Rails中使用Ajax:控制器呈现空文档?
我有一个页面,显示了Javascript 第一次尝试在Rails中使用Ajax:控制器呈现空文档?,javascript,ruby-on-rails,ajax,controller,Javascript,Ruby On Rails,Ajax,Controller,我有一个页面,显示了想法列表。每个项目旁边都有一个索赔按钮。应该发生的是,用户单击索赔按钮,Idea#索赔controller操作运行,索赔按钮变暗 实际上发生的是。。。没有什么。甚至连Rails错误页面都没有,这就是我被卡住的原因。相关片段如下: routes.rb resources :ideas do collection do post 'claim' end end ideas\u controller#索赔 def claim @i
想法列表
。每个项目旁边都有一个索赔按钮。应该发生的是,用户单击索赔按钮,Idea#索赔
controller操作运行,索赔按钮变暗
实际上发生的是。。。没有什么。甚至连Rails错误页面都没有,这就是我被卡住的原因。相关片段如下:
routes.rb
resources :ideas do
collection do
post 'claim'
end
end
ideas\u controller#索赔
def claim
@idea = Idea.find(params[:idea_id])
current_user.claim(@idea)
respond_to do |format|
format.js { render :nothing => true }
end
end
\u idea.html.erb partial
<span class="idea-actions" id=<%="idea"+idea.id.to_s%>>
<%= link_to "Claim", {method: "post", action: "claim", idea_id: idea.id}, {class: 'claim', remote: true} %>
</span>
这绝对是我第一次尝试这样做,而我的CoffeeScript/JS几乎不存在。如有任何建议或建议,将不胜感激
编辑1:将代码从application.js移到ideas.js.coffee,使用
按钮将表单切换到链接到。上面代码中显示的更改。现在,当我点击链接时,绝对没有发生任何事情。还是没有快乐!:\ 尝试将remote:true
移动到html\u选项
按钮的一部分
<%= button_to "Claim", {action: "claim", idea_id: idea.id}, {remote: true, class: 'btn btn-small claim'} %>
将按钮修改为更干净:
<%= button_to "Claim", claim_ideas_path(id: idea.id, format: 'js'), remote: true %>
那就行了 我这样做了(并更改为link_to,并做了一些其他更改),但它仍然不起作用。你能看看上面的代码变化,看看你是否还有其他见解吗?非常感谢。啊!!我还必须将POST操作移动到HTML选项!我想我现在在正确的轨道上!
<%= button_to "Claim", claim_ideas_path(id: idea.id, format: 'js'), remote: true %>