Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails creating malformed routes with dots

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]

like image 831
Nathan Buggia Avatar asked Dec 25 '12 20:12

Nathan Buggia


1 Answers

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 :)

like image 83
Mathieu Mahé Avatar answered Sep 24 '22 08:09

Mathieu Mahé