Today I tried to follow the basic "Twitter" tutorial on :
--> http://www.noupe.com/ajax/create-a-simple-twitter-app.html
But in the midle of the tutorial I have an issue.
It says that you should edit /config/routes.rb and add this piece of code :
ActionController::Routing::Routes.draw do |map|
map.resources :posts
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
It was written a while ago so there are probably incompatibilities with rails3 especially with the new routing synthax.
So I tried to fix changing it in :
Standart::Application.routes.draw do |map|
resources :posts
match ':controller/:action/:id'
match ':controller/:action/:id.:format'
end
Where "Standart" the name of the application is.
You need a root route:
resources :posts
root :to => 'posts#index'
You should try to avoid those catch-all routes that Rails 2 used. If you need other routes, try to see what fits into resourceful routes and use those, and create specific routes with the Rails 3 DSL for anything that doesn't fit.
Related
I have been working my way through the Railscasts videos and trying to adapt them for the latest
version of rails, being version 6. I am up to the following episode:
http://railscasts.com/episodes/124-beta-invitations
The video explains that you need to have some form of authorisation/authentication in place prior to the changes,
so I followed the instructions on the following site to do so:
https://www.nopio.com/blog/authentication-authorization-rails/
I stopped at Step 20 as the final step was not needed. Here devise is the main gem used for most
of the heavy lifting.
Now as far as the railscast goes I am at the point where we need to alter the signup path, which is where I am stuck, and the example given, of a change to routes file, is as follows:
map.signup '/signup/:invitation_token', :controller => 'users', :action => 'new'
There is also a comment from some time after going down the same path as me and using devise
which shows the following:
devise_for :users, :controllers => {:registrations => 'registrations'} do
get 'users/sign_up/:invitation_token' => 'devise/registrations#new', :as => "new_user_registration"
end
After entering the above and running rails routes, I cannot find any changes to the entry, ie. the addition of the :invitation_token and the sign_up path still looks as follows:
new_user_registration GET /users/sign_up(.:format) registrations#new
My understanding is it should look like:
new_user_registration GET /users/sign_up/:invitation_token(.:format) registrations#new
If someone could point in the right direction, it would be greatly appreciated :)
Please let me know if any additional information is required to assist in a solution?
as far as I'm aware devise doesn't let you modify the default registration path using devise_for (see the method docs: https://www.rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper%3adevise_for)
what you can do instead is use the devise_scope.
devise_scope :user do
post 'users/sign_up/:invitation_token', to: 'registrations#new', as: 'new_user_registration'
end
note that because you are using your own registrations controller you need to point the new path to use this controller, not the devise one.
I have a Rails 2.3.5 app with many controllers, models etc. which I'm trying to upgrade to Rails 3.2.21. I'm having some troubles with my routes. I tried to follow the Rails 3 new routing format but it doesn't seem to work. I'm getting two problems (which I guess all indicate one fundamental issue with my routing):
In the root ('/') I'm getting the generic "Welcome abroad" Rails
page. My routes (see below) have defined routing for root.
For some controllers I get No route matches [GET] "/study" message. My route shows this route, but for some reason doesn't define the GET method.
Here's my config/routes.rb code:
Myapp::Application.routes.draw do
root :to => 'study#index'
match 'login' => 'login', :protocol => 'https://'
resources :study_maps do
get :clone, :on => :member
end
# Route report create actions to the report controller
match 'report/create', :as => 'report'
match ':controller(/:action(/:id))(.:format)'
end
If I'm running rake routes I'm getting:
root / study#index
login /login(.:format) login#login {:protocol=>"https://"}
clone_study_map GET /study_maps/:id/clone(.:format) study_maps#clone
study_maps GET /study_maps(.:format) study_maps#index
POST /study_maps(.:format) study_maps#create
new_study_map GET /study_maps/new(.:format) study_maps#new
edit_study_map GET /study_maps/:id/edit(.:format) study_maps#edit
study_map GET /study_maps/:id(.:format) study_maps#show
PUT /study_maps/:id(.:format) study_maps#update
DELETE /study_maps/:id(.:format) study_maps#destroy
report /report/create(.:format) report#create
/:controller(/:action(/:id))(.:format) :controller#:action
Here's my StudyController#index code:
require 'myapp/studymgr'
require 'project_user'
require_dependency 'myapp/controller_extensions/report_manager'
class StudyController < ApplicationController
include PaginatorController
before_filter(:authenticate, :except => [:todo])
before_filter(:authorize,
:only => [:update, :destroy, :edit, :prune, :select_experiments ])
def index
#tags = Stag.find(:all).collect { |stag| stag.tag }
...
#include_ext = true
end
end
Can someone advise on what I'm missing?
Finally found a solution - my views had .rhtml extension. I found that this format is no longer supported under Rails 3 (What is the difference Between .erb , .rhtml and .html.erb?), so I changed all views extensions to .html.erb and then the routes worked with no need to specify explicit resource for each controller, just using the generic route: match ':controller(/:action(/:id))'.
As for the issue in the root route (#1 above), where I always got the Rails "Welcome abroad" page, despite having explicit route in my routes.rb, turns out I had to remove public/index.html which is loaded for root by Rails, and then my view was loaded.
I'd like to define a model as a resource to get all the REST URLs.
But, I'd like to disable some of the generated routes (e.g., DELETE). Is there an easy API for this, or do I just need to declare all the routes individually?
you have two ways of doing this
in config/routes.rb
1) as #emm, suggested define only the routes you want
2) use except keyword to exclude routes
Ex: Excluding destroy action
resources :books, :except => [:destroy]
read more here
HTH
Something like this in routes.rb:
resources :photos, :only => [:index, :show]
See more here.
You can also exclude specific actions like this:
resources :articles, except: :destroy
I have two models: team and project
routes.rb
resources :teams do
resource :projects
end
And two questions!
1- According to http://guides.rubyonrails.org/routing.html, I expect to get teams/:team_id/projects/:id path. However, this is not the case.
rake routes
team_projects POST /teams/:team_id/projects(.:format) projects#create
new_team_projects GET /teams/:team_id/projects/new(.:format) projects#new
edit_team_projects GET /teams/:team_id/projects/edit(.:format) projects#edit
GET /teams/:team_id/projects(.:format) projects#show
PUT /teams/:team_id/projects(.:format) projects#update
DELETE /teams/:team_id/projects(.:format) projects#destroy
so I had to name route to get it working
match 'teams/:team_id/projects/:id' => 'projects#show', :via => [:get], :as => :show_project
so how can take advantage of rails helper methods instead of naming them?
2- In project show action view, the debugger throws these parameters for me:
action: show
controller: projects
team_id: '1'
which is fine. but when I click on "new_team_projects_path" url, it redirects me to the same view and the debugger throws these parameters:
controller: projects
action: show
team_id: '1'
id: new
It doesn't redirect me to the new action, but it took "new" as an ID! why?
You need to use
resources :teams do
resources :projects
end
Note the plural! resource produces a singular route without id.
won't be relevant anymore with the first fix.
I'm working through Michael Hartl's excellent tutorial on Rails, but I am having trouble with exercise 7 in Chapter 11.
This exercise is:
Add a nested route so that
/users/1/microposts shows all the
microposts for user 1. (You will also
have to add a Microposts controller
index action and corresponding view.)
I've done this successfully by changing my routes.rb file to read:
resources :users do
resources :microposts, :only => [:create, :destroy]
end
I am able to successfully call /users/1/microposts from a browser. However, most of the tests in microposts_controller_spec.rb are now broken. I receive the "no route matches" error when running autotest. For instance, the first test, which simply reads:
it "should deny access to 'create'" do
post :create
response.should redirect_to(signin_path)
end
now produces the following error:
1) MicropostsController access
control should deny access to 'create'
Failure/Error: post :create
No route matches {:controller=>"microposts",
:action=>"create"}
When I check rake routes
, I find this entry:
user_microposts POST /users/:user_id/microposts(.:format) {:action=>"create", :controller=>"microposts"}
which suggests the route does exist.
Has anyone else run into this issue while completing the tutorial? Is there a change I need to make in the spec file once I introduce nested routes? Does Rspec work with nested routes?
thanks
Because this is a nested route you will need to pass the user_id through:
some_user = way_of_creating_a_user_goes_here
post :create, :user_id => some_user.id
RSpec will attempt to go to the /microposts route without this parameter.