Acts as follower Set Up - ruby-on-rails

I'm trying to get acts_as_follower working but i'm missing something. A user should be able to Follow another User.
I added the Gem :
gem "acts_as_follower", '~> 0.2.0' #0.2.0 for Rails 4
In my User model i added :
acts_as_follower
acts_as_followable
The User Controller i created looks like this :
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def follow
#user = User.find(params[:id])
current_user.follow(#user)
redirect_to :back
end
end
My routes :
App::Application.routes.draw do
root 'screens#index'
devise_for :users
get 'u/:id' => 'users#show', as: :user
resources :screens, :path => 's' do
member do
get :like
get :unlike
end
end
get "pages/home"
get "about" => "pages#about"
get "users/show"
end
now in the routes i have to add the member :follow =>
member do
get :follow
end
But i'm stuck at this point. I tried a few variations and my link to follow a User is :
<%= link_to "Follow", follow_user_path(#user)%>
and this is giving me the error ->
undefined method `follow_users_path' for #<#<Class:0x007fb4da78e920>:0x007fb4db3facb8>

The Solution was to create a user resource, it works perfectly now =>
resources :users do
member do
get :follow
get :unfollow
end
end

Related

Sending email, id is not passed

I have a page at
http://localhost:3000/email/correspond?id=6
It is a form to send an email. When sent it changes to:
http://localhost:3000/email/correspond
And throws up the error; Couldn't find User with 'id'= Which is caused by
def set_user
#user = User.find(params[:id])
end
which is in email_controller (see below)
I can't understand the problem because the id is given in the sending url.
Can anybody explain?
from email_controller.rb
`
def correspond
#cuser = #current_user
#recipient = #user
#title = "Email"
if param_posted?(:message)
#message = Message.new(params[:message])
if #message.valid?
PostMailer.say_hello(#cuser, #recipient, #message).deliver_now
flash[:notice] = "Email sent."
redirect_to user_path(#recipient)
end
end
end
def set_user
#user = User.find(params[:id])
end`
routes.rb
Eskvalleytales::Application.routes.draw do
root :to => 'users#index'
get 'user/:id' => 'user#show'
get 'user/search' => 'user#search'
resource :sessions
get 'session/destroy' => 'sessions#destroy'
get 'friendship/accept' => 'friendship#accept'
get 'friendship/decline' => 'friendship#decline'
get 'friendship/cancel' => 'friendship#cancel'
get 'friendship/delete' => 'friendship#delete'
get 'friendship/create' => 'friendship#create'
get 'email/correspond' => 'email#correspond'
post 'email/correspond' => 'email#correspond'
resources :comments
resources :users
resources :subcomments
resources :profiles
resources :emails
resources :users do
resources :comments do
resources :subcomments
end
end
resources :comments do
resources :subcomments
end
resources :users do
resources :emails
resources :new_file
end
resources :users do
resources :emails do
end
end
resources :users do
resources :fiendships
end
resources :users do
resources :subcomments
end
end
Firstly, You don't need to redirect on same action. If you still need than simple check for id in set_user method. Like following
def set_user
#user = User.find(params[:id]) if params[:id]
end
From your routes.rb, you have:
post 'email/correspond' => 'email#correspond'
That is what is responsible for the route (url) that you have when you send the form : http://localhost:3000/email/correspond
However, you are interested in sending the id along with the request as a params (/:id) and not as a query (?id=:id), since you are looking for this from inside the email controller.
To achieve this, you have to configure your route to take the given id along, as follow:
post 'email/correspond/:id' => 'email#correspond'
Doing this will ensure the id is sent as params, and then when you call your set_user
def set_user
#user = User.find(params[:id])
end
User.find(params[:id]) looks inside the request's params, and takes whatever is in the place of /:id .
Example:
a post request to "http://localhost:3000/email/correspond/6" will give you params[:id] => 6

Rails: How to render different versions of a "show" view

I have a model "User". I defined it in my routes.rb with resources :users. I want to be able to render different versions of the same user. To see if it would work, here's what I tried in my routes.rb:
get "users/:id_alt", :to => "users#alt", :as => :user
and in my controller:
def show
#user = User.find(params[:id])
end
def alt
#user = User.find(params[:id])
end
But when I navigated to users/1, I got this error:
ActiveRecord::RecordNotFound in UsersController#alt
Couldn't find User without an ID
and the error pointed to this line under def alt:
#user = User.find(params[:id])
Anyone know how to remedy this error or accomplish this another way?
It is because you have set your route to use :id_alt
get "users/:id_alt", :to => "users#alt", :as => :user
So the controller is assuming the value in that location should have the name :id_alt not :id. You are not going to be able to have both routes set up this way, but you could do this:
get "users/alt/:id_alt", :to => "users#alt", :as => :alt_user
get "users/:id", :to => "users#show", :as => :user
You will be able to use these path methods: alt_user_path and user_path
And your controller should look like this:
def show
#user = User.find(params[:id])
end
def alt
#user = User.find(params[:id_alt])
render :show
end
Running rake routes will result in this:
alt_user GET /users/alt/:id_alt(.:format) users#alt
user GET /users/:id(.:format) users#show

`Couldn't find User without an ID` with Friendly_ID gem

I am creating User Profiles with vanity URLs that use the username. The actual profile page works well, however I have my root page as the profile page if the user is signed in. If they are redirected to root then they get the error Couldn't find User without an ID and shows this code as the error pointing to the #user line...
class ProfilesController < ApplicationController
def show
#current_user = current_user
#user = User.friendly.find(params[:id])
#username = "#" + #user.username
#posting = Posting.new
end
end
Here is my routes file as well...
devise_for :users
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
get "profiles/show"
devise_scope :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
get '/edit', to: 'devise/registrations#edit', as: :edit
end
authenticated :user do
devise_scope :user do
root to: "profiles#show", :as => "authenticated"
end
end
unauthenticated do
devise_scope :user do
root to: "devise/sessions#new", :as => "unauthenticated"
end
end
get '/:id' => 'profiles#show', as: :profile
This is not the problem with friendly id gem. The problem is that you are redirecting to a show method without supplying id, hence params[:id] is nil.
You can go around this by changing your show method:
def show
#current_user = current_user # why do you need this?
#user = params[:id] ? User.friendly.find(params[:id]) : current_user
#username = "#" + #user.username
#posting = Posting.new
end

Rails Routing Error on Email Confirmation

I am new to rails and I am trying to add a email confirmation upon register. I currently get this error.
(Bonus points for any verbose and easily understood answer.)
Routing Error
No route matches {:action=>"edit", :controller=>"email_activations", :id=>false}
config/routes.rb
LootApp::Application.routes.draw do
get "password_resets/new"
get "sessions/new"
resources :users
resources :sessions
resources :password_resets
resources :email_activations
root to: 'static_pages#home'
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "registered", :from => "alain#private.com")
end
end
app/controllers/email_activations_controller.rb
class EmailActivationsController < ApplicationController
def edit
#user = User.find_by_email_activation_token!(params[:id])
#user.email_activation_token = true
redirect_to root_url, :notice => "Email has been verified."
end
end
app/views/user_mailer/registration_confirmation.html.haml
Confirm your email address please!
= edit_email_activation_url(#user.email_activation_token)
resources keyword in rails routes is a magical keyword that creates 7 restful routes by default
edit is one of those
check these docs link
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
edit expects to edit a record so requires a id to find the record for editing
in your case
you can just add a custom action in users controller
like
in UsersController
def accept_invitation
#user = User.find_by_email_activation_token!(params[:token])
#user.email_activation_token = true
redirect_to root_url, :notice => "Email has been verified."
end
in routes.rb
resources :users do
collection do
get :accept_invitation
end
end
in app/views/user_mailer/registration_confirmation.html.haml
accept_invitation_users_url({:token=>#user.email_activation_token})
Check out how to add custom routes here
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

How do I add a route which maps to a slug url generated by the stringex gem in ruby on rails 3.1?

It seems simple, in my model I have:
class CustomerAccount < ActiveRecord::Base
acts_as_url :name
def to_param
url # or whatever you set :url_attribute to
end
end
And in my controller, I have:
class CustomerAccountsController < ApplicationController
def show # dashboard for account, set as current account
#account = CustomerAccount.find_by_url params[:id]
no_permission_redirect if !#account.has_valid_user?(current_user)
set_current_account(#account)
#latest_contacts = Contact.latest_contacts(current_account)
end
end
What's currently in the routes.rb is:
resources :customer_accounts, :path => :customer_accounts.url do
member do
get 'disabled'
post 'update_billing'
end
end
That gives me the following error when I try to generate data via rake db:seed, or at least I assume the entry in routes is what's doing it.
undefined method `url' for :customer_accounts:Symbol
So what do I need to do to get the route set up? What I'd like is http://0.0.0.0/customeraccountname to map to the view for the customer account page.
UPDATE:
Here is the code that ended up working in routes.rb, which I discovered after looking at the examples in the answer below:
resources :customer_accounts, :path => '/:id' do
root :action => "show"
member do
get 'disabled'
post 'update_billing'
end
end
If you want to set it up so you have a route like you show, do this:
get '/:id', :to => "customer_accounts#show"
If you want the disabled and update_billing actions underneath this:
get '/:id/disabled', :to => "customer_accounts#disabled"
post '/:id/update_billing', :to => "customer_accounts#update_billing"
Alternatively (and much neater):
scope '/:id' do
controller "customer_accounts" do
root :action => "show"
get 'disabled'
get 'update_billing'
end
end

Resources