Javascript 只有在AJAX创建帖子后,才能将_链接到不匹配的路由
基本上,每次我使用AJAX创建帖子时,它都不会呈现新帖子,因为这行代码:Javascript 只有在AJAX创建帖子后,才能将_链接到不匹配的路由,javascript,ruby-on-rails,ajax,ruby-on-rails-3,Javascript,Ruby On Rails,Ajax,Ruby On Rails 3,基本上,每次我使用AJAX创建帖子时,它都不会呈现新帖子,因为这行代码: <%= link_to "promote", vote_scribble_path(:scribble_id => scribble.id, :vote => true), remote: true, method: :post%> 以下是正在运行的完整代码: create.js.erb在创建后更新帖子列表 /*Replace the html of the div post_lists wit
<%= link_to "promote", vote_scribble_path(:scribble_id => scribble.id, :vote => true), remote: true, method: :post%>
以下是正在运行的完整代码:
create.js.erb在创建后更新帖子列表
/*Replace the html of the div post_lists with the updated new one*/
$("#posts_list").html("<%= escape_javascript( render(:partial => "scribbles/scribbles") ) %>");
scribble\u controller.rb
def vote
@scribble = Scribble.find(params[:scribble_id])
@vote = params[:vote]
if @vote == "true"
@v = :up
current_user.vote(@scribble, :direction => @v)
else
current_user.unvote_for(@scribble)
end
respond_to do |format|
format.js{}
end
end
您应该在
链接中传递id
而不是scribble\u id
<%= link_to "promote", vote_scribble_path(:id => scribble.id, :vote => true), remote: true, method: :post%>
您可以通过运行rake routes
来检查
正如您所看到的,vote\u scribble\u path
需要一个id
占位符,而不是scribble\u id
,这就是为什么您会收到错误,因为Rails在应用程序中找不到与scribbles匹配的路由,所以您的路由建议您的控制器应为参数[:id]
,而不是参数[:scribble\u id]
。更改vote\u scribble\u路径(:scribble\u id=>scribble.id[…])
为vote\u scribble\u路径(:id=>scribble.id[…])
哈哈哈,谢谢!!!成功了。我可以接受晚一点的答复。你回答得太快了:没问题。很乐意帮忙:)
<% @scribbles.each do |scribble| %>
<%= link_to "promote", vote_scribble_path(:scribble_id => scribble.id, :vote => true), remote: true, method: :post%>
<%end%>
<%= link_to "promote", vote_scribble_path()%>
vote_scribble_path(:scribble_id => scribble.id, :vote => true), remote: true, method: :post, :put,:get %> tried them all still same error
resources :scribbles do
member do
post 'vote'
end
end
def vote
@scribble = Scribble.find(params[:scribble_id])
@vote = params[:vote]
if @vote == "true"
@v = :up
current_user.vote(@scribble, :direction => @v)
else
current_user.unvote_for(@scribble)
end
respond_to do |format|
format.js{}
end
end
<%= link_to "promote", vote_scribble_path(:id => scribble.id, :vote => true), remote: true, method: :post%>
vote_scribble POST /scribbles/:id/vote(.:format) scribbles#vote