小编Nat*_*gia的帖子

Rails用点创建格式错误的路线

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

http://localhost:3000/tweets.4
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:3000/tweets/4
Run Code Online (Sandbox Code Playgroud)

请注意它是如何使用点作为分隔符而不是预期的正斜杠.顶部链接无法解析为正确的视图,它只是重新加载/ 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 %>
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

routing ruby-on-rails

6
推荐指数
1
解决办法
2882
查看次数

标签 统计

routing ×1

ruby-on-rails ×1