Ruby on rails link_to syntax - ruby-on-rails

After following a tutorial Ive found. Im now redoing it again, without the scaffolding part, to learn it better.
However, editing my \app\views\home\index.html.erb to contain:
<h1>Rails test project</h1>
<%= link_to "my blog", posts_path>
I get an error:
undefined local variable or method `posts_path' for #<ActionView::Base:0x4e1d954>
Before I did this, I ran rake db:create, defined a migration class and ran rake db:migrate, everything without a problem.
So the database should contain a posts table. But that link_to command cant seem to find posts_path. That variable (or is it even a function?) is probably defined through the scaffold routine.
My question now is; how do I do that manually myself, define posts_path?

You will need to define a path to your posts in config/routes.rb
Rails 2.x syntax:
map.resources :posts
Rails 3.x syntax:
resources :posts

The _path methods are dynamically generated typically. The method missing error comes about when there isn't a route to the object specified or in this case the method you're calling explicitly.
Defining a route should fix this. HermanD above showed one way to do this.
You can run 'rake routes' from the root of your rails app to see all the routes that are configured

<%= link_to "my blog", posts_path>
If this is exactly what your erb contained, it's missing the percent sign at the end of the scriptlet element. Not sure if that caused your problem, or maybe I'm taking things too literally....

Related

No route matches [GET] "/reporting/from_perl/0" after upgrading to Rails 3.1

I'm upgrading a very old rails application step by step. At the moment I am stuck at Rails 3.1. I did all the relevant steps for upgrading. For now I don't want to use assets so I disabled it in config/application.rb.
As soon as a change the rails version in my Gemfile from 3.0.20 to 3.1.12 I get a no-route-matches error. I also changed all upgrading steps back to 3.0 to see which part causes the error but it happens only when I change the line in the Gemfile.
My routes.rb:
Wawi::Application.routes.draw do
match ":controller(/:action(/:id(.:format)))"
end
Please tell me if you need more code.
rake routes:
/:controller(/:action(/:id(.:format)))
(and a warning: circular argument reference)
Maybe another useful hint: The action is part of the Application Controller.
are you calling Object#to_i on your object inside your url helper? may be the object is nil and nil.to_i is always 0. And also note, rails primary id starts from 1.
So you should give a try with running rake routes:
ruby bundle exec rake routes
After running that command you should see your url list that are available based on your route file.
Thanks to a colleague I found the solution:
In application_controller.rb session :session_key => '...' had to be changed to Rails.application.config.session_options[:key] = '...'.

'Getting Started with Rails' tutorial section 5.7 - route syntax

It's probably me getting the wrong end of the stick, but here goes..
Following the Ruby on Rails tutorial here http://guides.rubyonrails.org/getting_started.html
(Ruby 2.0.0, Rails 4.0)
All good until I get to this section here:
If that's a route entry as the text suggests, then it doesn't look like the right syntax to me. I can make it work by changing it to this..
get '/posts/:id(.:format)' => 'post#show'
...haaang on... (penny drops)
Looking at it as I type this, it looks like the tutorial is showing the output of the rake routes command and expecting me to translate it to the valid route entry syntax?
(Given the copy/paste nature of the rest of the tutorial isn't that a little confusing for Ruby/Rails noobs like myself?)
Same question, different answer here Rails getting started 5.7
I guess that the doc needs some updating, since before it tells you to add
resources :posts
to your routes.rb configuration file, which should automatically add all 7 rest routes for you.
The guide tells you that the show action is absent in your controller. I think that the whole sentence should be reworded :)

Result of `rake routes` in ruby script

rake routes is very slow (30s in my computer) but I need it for routing spec.
So, is there a way to get all routes like rake routes? (or how rake routes works?)
I use Rails 3 and all I have seen is for Rails 3, and I found nothing I can use in the rails doc.
Rails.application.routes.routes.to_a
.to_a is optional, it just converts it to an array.
(I found this line in railties/lib/rails/tasks/routes.rake)
I use it like :
routes[10].defaults => {:action=>"edit", :controller=>"polls"}
Edit : You can find the (quite hacky) way I do my routing specs here : https://gist.github.com/52ac6d848ce0d9fd52ac
If you're using RSpec, you can use routing specs in your tests.
Another option is the rake shell; I love it.

Devise stack level too deep error

After install Devise I try to run
**rake db:migrate**
but it gives:
**rake aborted!
stack level too deep**
I'm on Ubuntu, changed
**ulimit -s unlimited**
and checked, it works, but still have the error.
I use RVM, tried to work out with Ruby1.9.2-p180, Ruby-1.9.2-p0, with Rails 3.0.9, Rails 3.1rc4, with Rubinius.
Tried with SQLite3 and with PostgreSQL.
Tried to uncomment as many as I can from the migration file.
Read all related Stackoverflow posts (and realized what I have is actually named StackOverflow).
Any help would be highly appriaciated! Many thanks
stack level too deep errors typically result from infinite recursion problems.
New Answer:
I forgot this was occurring during db:migrate. Is something in your users table migration relying on something that would rely on it?
Old Answer:
It would be helpful for you to show the lines of code you have in your routes.rb file for devise. For example, you might have:
devise_for :users, :controllers => { :registrations => "registration/foo" }
In this example, take a look at the new method in foo_controller.rb. If that method redirects to another controller that causes you to attempt to register again, you will have an infinite recursion.
The first thing I would do is look at what controllers are being called by putting some sort of debug output in your controllers. Try:
logger.debug("i am in foo")
or
puts "i am in bar"
If you can provide more information, I may be able to help more.
Have you tried bundle exec rake db:migrate?

I can't generate migrations - "illegal route the controller must be specified" - where am I going wrong?

Background: i'm using InstantRails 2.0
I wanted to add a new column to an existing table using the following syntax:
ruby script/generate migration add_fieldname_to_tablename fieldname:string
So I tried
ruby script/generate migration add_invites_to_user invites:integer
ruby script/generate migration add_invites_to_users invites:integer
And to test it further
ruby script/generate migration AddInvites
ruby script/generate migration AddInvites invites:integer
All of the above give me
builder.rb:175 in 'build': Illegal route: the :controller must be specified! (ArgumentError)
Got it,
I had specified a route without indicating the controller.
ie map.connect 'users/invite/:id'
I fixed it by adding :controller => 'users'
map.connect 'users/invite/:id', :controller => 'users'
I setup the first route while the server was running and it worked fine!
An explanation of why this happens helps:
When you run script/generate Rails will instantiate your application, which involves loading your routes amongst other things. This may seem excessive but it's "for the best", as the other things loaded in the Rails initialization process such as plugins, gems and initializers could affect how the migration operates.
So yes, if you have bad routing code it will break when you try to generate anything.

Resources