No route match even when route is defined - ruby-on-rails

I have just started working in rails.
I have a controller that redirects to another controller to send an email for emailid verification.
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
redirect_to :controller => 'verify', :action => 'sendmail'
else
format.html { render :new }
end
and this is the route in routes file
resources :verify
but still the program is returning an error
No route matches {:action=>"sendmail", :controller=>"verify"}
This is the code of the controller to which I am redirecting
def sendmail
# type = 1 for email and 2 for reseting password
randomNumber=rand()
verification[:value]=BCrypt::Password.create(randomNumber, :cost => 1)
verification[:type] = 1
if verification.save
Usermail.registered(#verification).deliver
end

I think Verify (singular) as a controller name won't easily be mapped by resources, instead just add a custom match
get '/verify/sendmail', to: 'verify#sendmail', as: :verification_mail
Then do
redirect_to verification_mail_path

Related

Controller not calling the View in Rails , is Sample

1 - My controller is this , but this not call the view whit this name is login.html.erb, im not understand beacause what happen this , is to show the form of the login.
class FinancesController < ApplicationController
# GET /finances
# GET /finances.json
def login
#user = User.find_by_name(params[:user])
if #user
session[:user_id] = #user.id
redirect_to :action => 'index'
else
redirect_to login_url
end
end
def index
#finances = Finance.all(:order => "created_at")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #finances }
end
end
2 - Someone can talk to me to use the authenticate method to validate a sample login whit user and password forms some, can show to me how use this method , the find_by_name_and_password dont works.
I thanks by the answers, many.
Routes.rb
Controle::Application.routes.draw do
match 'login' => 'finances#login'
resources :finances
end
check for the #user object.
because, as of your code it will redirect to your login method if #user is not available.
To render login.html.erb from login method, there is no need for any manual redirections.

how to route to a different page in ruby on rails

I have a session controller like
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
if you notice on successful sign in it does redirect_to user which is the show action in the User controller.
But say instead of that I have to go to the new action of user controller then how do i do that?
redirect_to user is just a shortcut for redirect_to url_for(user), which generates the url for a given resource (url_for) and then redirects to it.
If you want to redirect to another url, you can use the path helper.
redirect_to new_user_path
You could also use the url_for helper to generate an url.
redirect_to url_for(:controller => "users", :action => "new")
Which can be shortened to
redirect_to :controller => "users", :action => :new
You can even specify an url directly (redirect_to '/users/new') - which I would not recommend, as you can't change your routing later without changing all the urls.
The docs
As you can see, there are many ways to specify an url to redirect_to in rails. Take a look at the documentation for all of them
To render an action from another controller:
render 'users/new'
See: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

How to stay at same url when validation fails

I'm using Ruby on Rails 2.3.8 and I've got a registration form in which I receive a parameter as follows: /registration/4, which 4 is the id of a user who recommended the user that is about to register in the website.
The problem is that if the validation fails when the user submits the registation (the form renders to the controller users, action create_particular) the site will redirect to /users/create_particular, and therefore I lose the parameter with value 4 that I had before. Besides, I want the user to stay at the same url, which is /registration/4
How can I do that?
Then you should rewrite your create method. You should use redirect_to :back instead of render :action
UPD
def new
#word = Word.new(params[:word])
#word.valid? if params[:word]
end
def create
#word = Word.new(params[:word])
if #word.save
redirect_to #word
else
redirect_to new_word_path(:word => params[:word] )
end
end
Looks quite dirty, but this is just a scratch
UPD 2
This is really not the best solution, but it works
# routes.rb
match 'words/new' => 'words#create', :via => :post, :as => :create_word
# words_controller
def new
#word = Word.new
end
def create
#word = Word.new(params[:word])
respond_to do |format|
if #word.save
format.html { redirect_to(#word, :notice => 'Word was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
# views/words/new.html.erb
<%= form_for(#word, :url => create_word_path) do |f| %>
...
<% end %>
Submit to the current URI (e.g. action=""). When the submission is valid, redirect. POST->Redirect->GET is a good habit.
From the top of my head:
Edit your controller (registrations_controller.rb file). Create method by default contains following piece of code:
if #registration.save
format.html { }
format.xml { }
else
format.html { }
format.xml { }
end
Add redirect_to (:back) between brackets to else format.html{}
Ok I solved the problem by doing the following:
1) I created two routes with the same path, but with different conditions method (one it's post and the other one is set to get)
2) I changed the form in order to post to the POST action defined above
3) I added render => :my_action when the validation fails
So that's pretty much it.
Thanks anyway for all your help.
Hidden field. That user ID param has a name by which you extract it in your controller, right? So just put that value in a hidden field of the same name, then it will survive a round-trip.
For example:
<%= hidden_field_tag :referring_user_id, params[:referring_user_id] %>

Confused about Ruby on Rails REST Routing

I am really confused about Ruby on Rails REST routing. Even though I have specified that after the success it should go to the confirm action it goes to the show action and pass the ID=confirm.
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to :action => "confirm" }
else
format.html { render :action => "new" }
end
end
end
The error I get is the following:
ActiveRecord::RecordNotFound in ArticlesController#show
Couldn't find Article with ID=confirm
Rails.root: /Projects/highoncoding
Application Trace | Framework Trace | Full Trace
app/controllers/articles_controller.rb:31:in `show'
UPDATE 1:
Here is my Route.rb file:
resources :articles
get "articles/confirm"
# config/routes.rb
MyApp::Application.routes.draw do
resources :articles do
member do
get 'confirm'
end
end
end
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
# use a named route here
format.html { redirect_to confirm_article_url(#article) }
else
format.html { render :action => "new" }
end
end
end
end
you'll need to add the route so it looks like
match 'articles/confirm/', :controller => 'article', :action => 'confirm'
resources :articles
you need to have the :id in there or it will think that confirm is an id which is why you are seeing the error ID=confirm. make sure also that this is the first route. (at least before the resources for the articles controller.
You should probably add the confirm route directly in your routes file.
match 'articles/confirm' => 'articles#confirm'
resources only work for create/update/destroy/etc.

Rails 3 nested resources route error

I'm not being able to redirect the user to the page I want after he creates a new "service" resource.
Here's the routes.rb:
resources :wsps do
resources :services
end
The html form:
<%= form_for([#wsp,#service]) do |f| %>
Services_controller.rb:
def new
#wsp = current_wsp
#service = #wsp.services.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #service }
end
end
def create
#wsp = current_wsp
#service = #wsp.services.build(params[:service])
if #service.save
redirect_to wsp_service_path
end
end
The wsp_service_path goes to /wsps/1/services and the error:
No route matches {:action=>"destroy", :controller=>"services"}
What am I doing wrong? Why cant't I use "wsp_service_path"?
Thank you.
You can use wsp_service_path (while you should be use wsp_service_url since you are in a controller). All you are missing is arguments. wsp_service_path (or _url) are going to expect two arguments: a wsp and a service. Once you provide those two, it works.
redirect_to wsp_service_url(#wsp, #service)

Resources