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
Related
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
Im getting this error after I submit the form.
undefined method `user_url' for #
I am not using the resources :users in my route file and i guess thats why i get the error.
My routes.rb
TaskManager::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
get 'users/new' => 'users#new'
post 'users/' => 'users#create'
post 'users/:first_name' => 'users#show'
end
and my users_controller.rb
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
#user.save
if #user.save
redirect_to #user
else
end
end
def show
#user = User.find(params[:first_name])
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email)
end
end
From your code, I'm guessing that the route you want is the post 'users/' => 'users#create'. To Achieve that, I'll you should need to do is change that line to post 'users/' => 'users#create', :as => 'user'.
For future reference: http://guides.rubyonrails.org/routing.html
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
I'm getting the following error on a page that contains a link to credit/edit an object:
Routing Error
No route matches {:controller=>"connections", :action=>"edit", :id=>nil}
My app has user profile pages (#show action in users_controller.rb) with a link to create or edit an existing "connection" (a relationship between users like Facebook or LinkedIn). If the connection does not exist, the link will send the user to create a new connection. If the connection already exists, the link will send the user to edit the existing connection. My create functionality works fine. However, once the connection has been created, visiting the user page throws the routing error. It clearly does not like my link_to for the edit action.
Here is the link_to on the user page:
<% unless current_user == #user %>
<% if #contact.present? %>
<%= #user.first_name %> is your <%= #connection.description %> (<%= link_to "edit contact", { :controller => 'connections', :action => 'edit', :id => #connection.id} %> )
<% else %>
How do you know <%= #user.first_name %>? (<%= link_to "edit contact", { :controller => 'connections', :action => 'create', :user_id => #user.id }, :method => 'post' %> )
<% end %>
The error also specifically states that :id is set to nil (null). The problem must be here. How can I my app to refer to the correct connection? My instance variable should have done the trick.
Here's the connection controller:
class ConnectionsController < ApplicationController
def index
end
def update
#connection = current_user.connections.find(params[:id])
if #connection.update_attributes(params[:connection])
flash[:notice] = "Contact relationship updated"
render 'activities'
end
end
def edit
#connection = current_user.connections.find(params[:id])
end
def create
##user = User.find(params[:user_id])
#connection = current_user.connections.build(params[:connection])
#user = User.find(params[:user_id])
if #connection.save
flash[:success] = "Contact relationship saved!"
else
render #user
end
end
end
And here's the User controller show action (where the link_to exists):
def show
#user = User.find(params[:id])
#connection = current_user.connections.build(:otheruser_id => #user)
#contact = current_user.connections.where(user_id: current_user, otheruser_id: #user)
end
routes.rb:
authenticated :user do
root :to => 'activities#index'
end
root :to => "home#index"
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users do
member do
get :following, :followers, :posts, :comments, :activities, :works, :contributions, :connections
end
end
resources :works do
resources :comments
end
resources :relationships, only: [:create, :destroy]
resources :posts
resources :activities
resources :reposts
resources :comments do
member do
put :toggle_is_contribution
end
end
resources :explore
resources :connections
Any ideas on how to fix the link_to? Thanks!
EDIT 1: relevant rake routes:
connections GET /connections(.:format) connections#index
POST /connections(.:format) connections#create
new_connection GET /connections/new(.:format) connections#new
edit_connection GET /connections/:id/edit(.:format) connections#edit
connection GET /connections/:id(.:format) connections#show
PUT /connections/:id(.:format) connections#update
DELETE /connections/:id(.:format) connections#destroy
Your problem is in:
#connection = current_user.connections.build(:otheruser_id => #user)
That instantiates a connection object and assigns it to #connection. This object hasn't been saved, therefore it doesn't have an id. Conceptually it's wrong too, how can you edit something that doesn't exist yet? If you want to send the user to create a new connection, Connections#new is what you're after.
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