-1

I am new in Ruby on Rails. I have been trying for two days to define a route for a method in the controller but error shows saying "No route matches [GET]". Here is the code

donations Controller:

before_action :set_donation, only: [:show, :edit, :update, :destroy, :create_user_account]
  before_action :set_campaign, only: [:new, :create_user_account]
//this is the method that i want to call 
  def create_user_account
  end

Here is my route file

Rails.application.routes.draw do
  resources :donations, except: [:new, :create]
 get 'donations/create_user_account' => 'donations#create_user_account'
  resources :campaigns do
    resources :donations, only: [:new, :create, :create_user_account]
    get 'donations/create_user_account' => 'donations#create_user_account'
  end
  resources :organizations

  devise_for :users

  root to: "campaigns#latest"
end

The routes are showing my route name but when i hit the route "no matching" route error occurs.

route1: campaign_donations_create_user_account_path GET /campaigns/:campaign_id/donations/create_user_account(.:format) donations#create_user_account route2:

donations_create_user_account_path  GET /donations/create_user_account(.:format)    
donations#create_user_account

I want to call route 2 but no route is working

I call my route 2 like this

http://localhost:3000/donations/create_uer_account

This is the error enter image description here

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
john
  • 611
  • 1
  • 7
  • 33
  • You are doing it wrong, you should read route types. http://stackoverflow.com/questions/3028653/difference-between-collection-route-and-member-route-in-ruby-on-rails OR send ID as query parameter in url. – Malik Shahzad Apr 15 '17 at 10:55
  • you are attaching an error related to the typo create_uer_account ... – catch22 Apr 15 '17 at 10:59
  • 1
    @jenvvv edited but same error that is not the problem at all. – john Apr 15 '17 at 11:02

1 Answers1

1

The error is because this line

resources :donations, except: [:new, :create]

is before the others in routes.rb. Rails matches with the first route it finds in the routes.rb file.

It should look something like this

get 'donations/create_user_account' => 'donations#create_user_account'
resources :donations, except: [:new, :create]

then Rails matches with your create_user_account first.

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • after changing the code according to your advice the error is showing ActiveRecord::RecordNotFound in DonationsController#create_user_account Couldn't find Donation with 'id'= def set_donation "@donation = Donation.find(params[:id])" end Why it is always going inside set_donation method??? – john Apr 15 '17 at 11:20
  • Because you have it declared in a `before_action` – Eyeslandic Apr 15 '17 at 11:20
  • 1
    still the same error and my code is before_action :set_donation, only: [:create_user_account, :show, :edit, :update, :destroy] – john Apr 15 '17 at 11:24
  • It's calling `set_donation` with that `before_action`, remove the `create_user_account` from the `before_action` – Eyeslandic Apr 15 '17 at 11:25
  • 1
    Thanks a lot for your help. I will bother you in future too. :) – john Apr 15 '17 at 11:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141773/discussion-between-john-and-iceman). – john Apr 15 '17 at 13:52