Rails用点创建畸形的路线 [英] Rails creating malformed routes with dots

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

问题描述

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

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

http://localhost:3000/tweets/4

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

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'

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

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

推荐答案

您是否尝试过 tweet_path user_path

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

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

可以肯定的是,尝试使用 Rake路线在控制台中。

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

编辑:
您还忘记添加 resources:users 在您的路线文件中:)

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

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

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