Reputation: 42019
I'm getting a no-route matches error in a Rails application that uses omniauth to sign in with Twitter. I started the application following along with a Railscast, in which the logged in Twitter user was created in a sessions controller like this
class SessionsController < ApplicationController
def create
twitteruser = Twitteruser.from_omniauth(env["omniauth.auth"])
session[:twitteruser_id] = twitteruser.id
redirect_to twitterquestions_url, notice: "Signed in!"
end
I created a table to store the users Twitter information like this
def change
create_table :twitterusers do |t|
t.string :provider
t.string :uid
t.string :name
t.string :image
t.timestamps
end
end
this is a databaseentry
=> [#<Twitteruser id: 1, provider: "twitter", uid: "1121308772", name: "mytwitteraccou t", image: "http://a0.twimg.com/profile_images/3164285746/.", created_at: "2013-02-12 23:52:44", updated_at: "2013-02-12 23:52:44">]
I also created a Twitterusers controller like this so that I could show a user
class TwitterusersController < ApplicationController
def show
@twitteruser = Twitteruser.find(params[:id])
end
end
In the routes file
resources :twitterusers
Based on the output of rake routes, I created a link to the currentusers profile like this (and also added a show.html.erb file in the view)
<li>Signed in as <%= link_to "current_user.name", twitteruser_path %>!</li>
Rake routes
twitterusers GET /twitterusers(.:format) twitterusers#index
POST /twitterusers(.:format) twitterusers#create
new_twitteruser GET /twitterusers/new(.:format) twitterusers#new
edit_twitteruser GET /twitterusers/:id/edit(.:format) twitterusers#edit
twitteruser GET /twitterusers/:id(.:format) twitterusers#show
PUT /twitterusers/:id(.:format) twitterusers#update
DELETE /twitterusers/:id(.:format) twitterusers#destroy
When I got that error, I wondered if it had to do with the fact that the user was created in sessions_controller, so I then created a show action in the sessions controller
sessions_controller
def show
@twitteruser = Twitteruser.find(params[:id])
end
and created a sessions resources in routes
resources :sessions
and then based on rake routes I created the following link
but got the same error
sessions GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PUT /sessions/:id(.:format) sessions#update
DELETE
There were already these routes in session controller
match 'auth/:provider/callback', to: 'sessions#create'
match 'auth/failure', to: redirect('/')
match 'signout', to: 'sessions#destroy', as: 'signout'
Can someone point out what I'm doing wrong?
Upvotes: 0
Views: 85
Reputation: 35531
Try changing:
<%= link_to "current_user.name", twitteruser_path %>
to:
<%= link_to current_user.name, twitteruser_path(current_user) %>
and modify this section to only render if current_user
is populated:
<% if current_user %>
Your link here
<% end %>
Upvotes: 2