ruby-on-rails - Rails 创建带点的畸形路线

标签 ruby-on-rails routing

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

http://localhost:3000/tweets.4

当我期望它们的格式如下时:
http://localhost:3000/tweets/4

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

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

我将不胜感激任何人可以提供的任何帮助或指示!

以下是相关的源文件和版本信息:

推文/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

路线.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]

最佳答案

你试过了吗tweet_pathuser_path ?

您想访问 show 操作。对于该操作,*_path 调用中的模型名称必须是单数。

可以肯定的是,试试 rake routes在控制台中。

编辑:
你也忘了加resources :users在您的路线文件中:)

关于ruby-on-rails - Rails 创建带点的畸形路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14033853/

相关文章:

mysql - 尝试为 rails 安装 mysql 支持时出错

ruby-on-rails - 如何实现一个基于订阅的DB,比如basecamp

php - 如何在软删除模型上使用资源 Controller 的 show 方法?

scala - 将消息转发到下一个 Round Robin 路由器

javascript - 为什么我的 CoffeeScript 函数没有执行?

css - 可见性规则在媒体查询中不起作用

asp.net - 在 HttpModule 中获取 Asp.net Web API Controller 上下文或操作上下文

asp.net-mvc - ASP.NET MVC 路由和静态数据(即图像、脚本等)

php - Laravel 5 检查当前 url 是否匹配 Action

javascript - Rails Assets Pipeline 从 Controller 和方法加载 JavaScript