'Getting Started with Rails' tutorial section 5.7 - route syntax - ruby-on-rails

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

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] = '...'.

NoMethodError undefined method '-#' NoMethodError in Controller Ruby on Rails

Context:
I pulled the most recent code from the repository and tried to make sure that the changes I was about to push up were going to work with that version of the code. This is a Ruby on Rails application. Also worth noting is the fact that when running the main application that I pulled from on the web, this error does not show up. But if I run my branch or the main branch cloned onto my environment, the error always shows up for every url I try. So it is on my end.
Problem:
As soon as I go to localhost:3000, I get the following error:
NoMethodError in HomeController#index
undefined method `-#' for #<ActionDispatch::Response:0x64fd460>
What I've Tried:
I have asked my question on the #rubyonrails IRC channel and nobody was able to determine what was going on through the Full Trace (I haven't posted it here because I wasn't sure what was the best way to do that on here; it didn't look very good in the code block or block quote). I have looked at my HomeController's index method, which is defined as such:
def index
#groups = #current_user.groups
#things = Thing.where(:group_id => #groups.map{|e|e.id})
end
I have also Googled around and haven't found what I need to fix the problem.
What I've Learned So Far:
-# is an operator. Some people may receive a similar error in assuming that Ruby has the shortcut to
variable = variable + 1
that a lot of other languages have:
variable++
Here is an example of that case: Undefined method `+#' for false:FalseClass (NoMethodError) ruby
Question:
Does anyone have any further suggestions on how to find the issue here? Also, if I could easily put the Full Trace on here, formatted in an aesthetically pleasing manner, would someone tell me how? I'm at a loss with this one :(
Update (2/8/2013):
It seems that the issue does not necessarily reside in the HomeController nor home/index.html.erb View. I have attempted to access ANY url with a valid action and the same error occurs with "NoMethodError in..." changing to the corresponding [...]Controller#index.
Update (2/9/2013):
Since this error happens no matter what url I try to navigate to, I decided to look in the routes.rb file in the config folder. I ran my server through rubymine instead of the command line this time, which made it a little easier to read for me. I started looking through all the spit out and I noticed an interested line that consisted of:
["private-key looking thing"] [127.0.0.1] Started GET "/" for 127.0.0.1 at 2013-02-09 18:20:52 -0700
It seems like there is a syntactical error in routes.rb (that's my best guess at this point). This does not explain why this only is an issue on my local environment with the same code sets, but what else do I have to go off of?
Does anyone have any suggested things to be on the look out for while I sift through this file? Not really sure what to be looking for as far as errors are concerned. Rubymines inspection stuff converted all my double quotes to single quotes and doesn't really have anything else to complain about.
Thanks in advance,
Jake Smith
I am guessing it might as well be an syntactical error in the corresponding view page Home/index.html.haml .. I am suspecting there is unintended '-' in front of variable call. I tried to simulate a similar scenario in my rails platform and see following page on browser
undefined method `-#' for false:FalseClass
Correct lines of code
%h1 All Movies
= "filtervalue=#{#isFilterOld}"
= "Sortvalue=#{#isSortOld}"
Edited to simulate the error (observe the - in front of isFilterOld variable)
%h1 All Movies
= "filtervalue=#{-#isFilterOld}"
= "Sortvalue=#{#isSortOld}"
I have fixed the issue!
What fixed it:
Go to the directory where your gems are (for me that was C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1)
Delete all gems except for bundler
Make sure you delete the gems from the /cache/, /gems/, and /specifications/ folders (I just deleted them from the /gems/ folder at first and bundle install indicated that it could still find the gems)
Run bundle install
Further Inquiry:
Does anybody have any idea why this worked? I don't know if at this point I can narrow down which gem was causing the issue because the app is working now (I can visit all the urls with corresponding views). If the issue comes up again, I will delete gems one by one to nail down which one was at least causing the issue for me. But if anyone has any insight on this, a more detailed answer would be greatly appreciated by many more people than just me, I think. Thanks to all who helped thus far!

Rails Controller not picking up matching models and methods

So my Rails controller isn't working (properly) for some reason.
I first noticed something funny when I opened the controller file for the first time.
Compare this to normal syntax highlighting. (Also notice the overly long name)
This was the error I got when I tried to create a link in one of my views.
ActionView::Template::Error (undefined method `new_voyage_at_port_log_entries_path' for #<#<Class:0x007fb1c5921a98>:0x007fb1c81a32a0>):
Here's proof that I inserted this in my routes.
resources :voyage_at_port_log_entries
I do have it working by creating manual routes, but that is a little awkward, and I may forsee problems later. Why might ActionView not be working?
DUH! Routes are created for the singular version.
So...
new_voyage_at_port_log_entry_path
works!
BUT...syntax highlighting is still off...
Hmmm...not important but weird...probably has a different pluralization engine?

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?

Ruby on rails link_to syntax

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....

Resources