更新的代码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教程,看看在解决这个问题时我是否无法弄清楚。因为我打算接下来要做的事情(我计划稍后将两个应用程序结合起来),现在看来是时候了。
可以重述正是问题低于你更新后的代码是什么? – s84 2010-11-07 23:20:35
与场景的描述和详细的问题 – tehaaron 2010-11-08 00:02:02