加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails用点创建格式错误的路由

发布时间:2020-12-16 20:50:53 所属栏目:百科 来源:网络整理
导读:我正在使用路径帮助器方法在link_to中生成URL,并且它们返回如下格式化的URL: http://localhost:3000/tweets.4 当我期待它们像这样形成时: http://localhost:3000/tweets/4 请注意它是如何使用点作为分隔符而不是预期的正斜杠.顶部链接无法解析为正确的视图
我正在使用路径帮助器方法在link_to中生成URL,并且它们返回如下格式化的URL:
http://localhost:3000/tweets.4

当我期待它们像这样形成时:

http://localhost:3000/tweets/4

请注意它是如何使用点作为分隔符而不是预期的正斜杠.顶部链接无法解析为正确的视图,它只是重新加载/ tweets视图.当我手动将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

的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]

解决方法

你试过tweet_path和user_path吗?

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

可以肯定的是,在控制台中尝试rake路由.

编辑:您还忘记添加资源:路由文件中的用户:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读