阿贾克斯
更新的代码Rails3中路由错误在底部阿贾克斯
我创建一个故事表决通过应用Rails的简单的2本书。我得到这个错误,当我按一下按钮投了一个故事:
No route matches "/stories/4-pure-css-icons-showcase"
我的路由文件看起来是这样的:
Shovell::Application.routes.draw do
get "votes/create"
root :to => "stories#index"
resources :stories do
resources :votes
end
end
votes_controller.rb:
class VotesController < ApplicationController
def create
@story = Story.find(params[:story_id])
@story.votes.create
end
end
create.rsj:
page.replace_html 'vote_score', "Score: #{@story.votes.size}"
page[:vote_score].visual_effect :highlight
show.html.erb:
<h2>
<span id="vote_score">
Score: <%= @story.votes.size %>
</span>
<%= @story.name %>
</h2>
<p>
<%= link_to @story.link, @story.link %>
</p>
<div id="vote_form">
<%= form_tag :url => story_votes_path(@story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
</div>
story.rb:
class Story < ActiveRecord::Base
validates_presence_of :name, :link
has_many :votes
def to_param
"#{id}-#{name.gsub(/\W/, '-').downcase}"
end
end
我已经通过一些其他错误的工作在这之前有做不推荐使用的代码等等,等等我现在感觉有些失落。它似乎应该只是一个路由问题,但由于我一直在处理AJAX错误,这也与投票功能有关,所以我想发布这些文件,以防万一它不仅仅是路由。
它说没有路由匹配"/stories/4-pure-css-icons-showcase"
但是当我访问"/stories"
(我的根),然后单击链接带我去"/stories/4-pure-css-icons-showcase"
它工作正常,但是点击投票按钮,我得到这个错误后。正如您在阅读完代码后可能知道的那样,假设更新投票计数并通过ajax执行:highlight
。
UPDATE:
改变的代码(所有的改变都是每山姆的建议):
路线:
Shovell::Application.routes.draw do
resources :votes
root :to => "stories#index"
resources :stories do
resources :votes
end
show.html.erb:
<div id="vote_form">
<%= form_tag :url => new_story_vote_path(@story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
</div>
votes_controller.rb
class VotesController < ApplicationController
def create
@story = Story.find(params[:story_id])
@story.votes.create
respond_to do |format|
format.html
format.js
end
end
的问题仍然是完全一样的,但我认为(读:希望如此),我们都在进步!
场景:我的指数(/stories
)页面随机显示此页它的数据库,当你点击它把你的链接故事的内部页面(前/stories/2-sitepoint-forums
)一个故事显示的票数这个故事有一个按钮可以为它投票。当您点击投票按钮时,它会使用ajax更新@story.vote.size
并使用:highlight
视觉效果。然而,问题是,当你点击投票按钮的页面变为“路由错误”网页,其中显示:
No route matches "/stories/2-sitepoint-forums"
其怪我,因为你其实可以路由到该地址和你从第一页上的链接...
下面是在控制台上的错误:
Started POST "/stories/2-sitepoint-forums?url=%2Fstories%2F2-sitepoint-forums%2F
votes%2F2-sitepoint-forums&remote=true" for 127.0.0.1 at 2010-11-08 16:30:17 -08
00
ActionController::RoutingError (No route matches "/stories/2-sitepoint-forums"):
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0.rc2/lib/action_dis
patch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0m
s)
林不知道这是否更具说服力,但我想我会添加它incase。
新: 我一直没能解决这个问题的呢。因为我仍然不觉得自己完全理解了这个问题,所以我决定转到在线Ruby on Rails 3教程,看看在解决这个问题时我是否无法弄清楚。因为我打算接下来要做的事情(我计划稍后将两个应用程序结合起来),现在看来是时候了。
<%= form_tag :url => new_story_vote_path(@story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
这应该发送给创建操作。
def create
@story = Story.find(params[:story_id])
@story.votes.create
respond_to do |format|
format.html
format.js
end
end
这应该照顾你的ajax。
借此get "votes/create"
出你的路线
,并添加此
map.resources :votes
更新它应该是create_story_vote_path?除此之外,看起来不错。我想,这里最初的问题是发布到GET URL。 – 2010-11-07 22:05:27
我仍然收到相同的错误。我完全按照Sam的说法以及Paul评论的内容进行了尝试。此外,我改变了“投票”到“投票”,只是为了确保它没有帮助。一个问题,但我没有format.html或format.js我应该? – tehaaron 2010-11-07 22:12:51
'“的地图。”'在Rails3中被改变,所以我做了相当的'资源:votes',现在我有一个新的错误,生病更新主哨吧。 – tehaaron 2010-11-07 22:43:24
我不熟悉使用的form_tag和:远程(我通常只是写的jQuery像这样的东西)但是有一些事情肯定会在我身上弹出,你在这里做的事情可能会帮助你解决问题。
首先,我认为您可以重新设定您设置投票的方式,从而设置投票表格的方式。在故事的控制器,为展会的行动,我会成立了投票的时候了:
@vote = Vote.new(:story_id => @story.id)
这可让您设置形式,所以:
= form_for(@vote), :remote => true do |f|
= hidden_field f.story_id
= submit_tag "Vote"
这既是一个更清洁的方式在我看来,也可能会解决您正在处理的一般问题,因为您现在将传递数据与您的POST请求中的表单(隐藏字段)。如果您执行实际上未提交数据的AJAX POST请求,Rails将表现异常。
换句话说,你原来的形式很可能运行作为一个AJAX POST请求,但它会更好地工作作为一个AJAX的GET请求,因为它实际上没有提交数据,这只不过是“打”的URL。
不应该通过'POST'(和'DELETE/PUT',如果它是一个选项!)而不是' GET'? – Zabba 2010-11-08 02:35:46
当我说“更好地工作”时,我的意思是,它实际上是从技术角度来看,而不是一定应该这样做。他发送了一个GET请求,但创建了一个POST的表单,这可能是最初的根本问题。请注意,我建议的解决方案是一个POST请求,这是从资源的角度来看它应该如何完成的。 – adriandz 2010-11-08 02:52:56
我是编程新手,因为这是我尝试学习的第一门语言。我不完全理解你对GET和POST等的看法,但明天我会花更多的时间来理解它。我更担心现在编写最佳实践的方法,而不是简单地将其运用到工作中。 – tehaaron 2010-11-08 06:39:02
我不确定你是否找到了你的问题的答案,但我想为其他人发布可能正在寻找类似于你的答案。
代码:
<%= form_tag :url => new_story_vote_path(@story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
将导致/故事/:POST请求:与story_id /票/新的URL。它不会工作,因为new
路由是一个:get方法请求。如果你想去new
方法,你需要告诉表单使用get http方法。
<%= form_tag :url => new_story_vote_path(@story), :remote => true, :html => { :method => :get } do %>
<%= submit_tag 'shove it' %>
<% end %>
但是,我认为你想要在你的控制器中路由到create
方法。我会做这样的事情:
<%= form_tag :url => story_votes_path(@story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
这应该正确地路由到您的VotesController的create
方法。
可以重述正是问题低于你更新后的代码是什么? – s84 2010-11-07 23:20:35
与场景的描述和详细的问题 – tehaaron 2010-11-08 00:02:02