Rails 用点创建格式错误的路线 [英] Rails creating malformed routes with dots

查看:9
本文介绍了Rails 用点创建格式错误的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用路径辅助方法在 link_to 中生成 URL,它们返回的 URL 格式如下:

http://localhost:3000/tweets.4

当我期望它们像这样格式化时:

http://localhost:3000/tweets/4

注意它是如何使用点作为分隔符而不是预期的正斜杠的.顶部链接无法解析为正确的视图,它只是重新加载/tweets 视图.当我手动将 URL 编辑为底部时,它会打开正确的/tweets/show/.

我在在线研究中发现的最接近的事情是人们遇到了错误嵌套的路由语句 - 但我不认为我在这里这样做.

如果有人能提供任何帮助或指点,我将不胜感激!

这里是相关的源文件和版本信息:

tweets/index.html.erb

<h1>列出推文</h1><% @tweets.each 做 |tweet|%>

<!-- 以/tweets.2 的格式创建路径 --><div><%= link_to tweet.status, tweets_path(tweet) %></div><!-- 以/user.1 的格式创建路径 --><div><%= link_to tweet.user.name, users_path(tweet.user) %></div></div><%结束%>

tweets_controller.rb

类 TweetsController <应用控制器定义索引@tweets = Tweet.all结尾定义显示@tweet = Tweet.find(参数[:id])结尾定义新@tweet = Tweet.new结尾定义创建@tweet = Tweet.new(params[:tweet])@tweet.user = User.last如果(@tweet.save)重定向到:根结尾结尾定义编辑@tweet = Tweet.find(参数[:id])结尾默认删除结尾结尾

routes.rb

Zombietweets::Application.routes.draw 做资源:推文根:to =>'推文#索引'结尾

宝石文件

来源 'https://rubygems.org'宝石'导轨','3.2.9'组:开发,:测试做宝石'sqlite3','1.3.5'宝石'rspec-rails','2.11.0'结尾组:资产做宝石'sass-rails','3.2.3'宝石'咖啡轨','3.2.1'宝石'uglifier','1.0.3'结尾宝石'jquery-rails','2.0.2'

我正在使用 Rails 3.2.9 和 Ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]

解决方案

你试过 tweet_pathuser_path 吗?

您想要访问显示操作.对于该操作,模型名称在 *_path 调用中必须是单数.

为了确定,请在控制台中尝试 rake routes.

您也忘记在路由文件中添加 resources :users :)

I'm using the path helper methods to generate URLs in link_to, and they are returning URLs formated like this :

http://localhost:3000/tweets.4

when I was expecting them to be formated like this:

http://localhost:3000/tweets/4

Note how it is using a dot as the delimiter instead of the expected forward slash. The top link doesn't resolve to the correct view, it simply reloads the /tweets view. When I manually edit the URL to be like the bottom, it opens the correct /tweets/show/.

The closest thing I found in my online research was that people encountered this with wrongly nested routing statements - but I don't think I'm doing that here.

I would appreciate any help or pointers anyone can provide!

Here are the related source files and version information :

tweets/index.html.erb

<h1>Listing tweets</h1>

<% @tweets.each do |tweet| %>
<div>
    <!-- creates path in format of /tweets.2 -->
    <div><%= link_to tweet.status, tweets_path(tweet) %></div>

    <!-- creates path in the format of /user.1 -->
    <div><%= link_to tweet.user.name, users_path(tweet.user) %></div>   
</div>
<% end %>

tweets_controller.rb

class TweetsController < ApplicationController

  def index
    @tweets = Tweet.all
  end

  def show
    @tweet = Tweet.find(params[:id])
  end

  def new
    @tweet = Tweet.new
  end

  def create
    @tweet = Tweet.new(params[:tweet])
    @tweet.user = User.last

    if(@tweet.save)
      redirect_to :root
    end  
  end

  def edit
    @tweet = Tweet.find(params[:id])
  end

  def delete
  end

end

routes.rb

Zombietweets::Application.routes.draw do
  resources :tweets
  root :to => 'tweets#index'
end  

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.9'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.11.0'
end

group :assets do
  gem 'sass-rails',   '3.2.3'
  gem 'coffee-rails', '3.2.1'
  gem 'uglifier', '1.0.3'
end

gem 'jquery-rails', '2.0.2'

I'm using Rails 3.2.9 and Ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]

解决方案

Have you tried tweet_path and user_path ?

You want to access the show action. For that action, the model name must be singular in the *_path call.

To be sure, try a rake routes in a console.

EDIT: You also forget to add resources :users in your routes file :)

这篇关于Rails 用点创建格式错误的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆