user3576036
user3576036

Reputation: 1435

No route matches [GET] "/controller/method"

I am trying to call a controller action on the link on rails app.

The controller action basically creates a new phone number and generates a Twilio pin.

controller

def resend
      @phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number])
      @phone_number.generate_pin
      @phone_number.send_pin
      respond_to do |format|
        format.js # render app/views/phone_numbers/create.js.erb
      end
end

view

<a href="<%= phone_numbers_resend_path %>">Resend Pin</a>

routes.rb`

post 'phone_numbers/resend' => "phone_numbers#resend"

So when I click "Resend Pin". I am getting

No route matches [GET] "/phone_numbers/resend"

rake routes output

phone_numbers POST /phone_numbers(.:format)        phone_numbers#create
    new_phone_number GET  /phone_numbers/new(.:format)    phone_numbers#new
phone_numbers_verify POST /phone_numbers/verify(.:format) phone_numbers#verify
phone_numbers_resend POST /phone_numbers/resend(.:format) phone_numbers#resend

In routes, I have set it as a post. Why am I getting this? How can I fix this?

Upvotes: 0

Views: 104

Answers (2)

Chakreshwar Sharma
Chakreshwar Sharma

Reputation: 2610

Anchor tag(<a>) by default uses get request . But in routes, you are using post method. So, to make it working you can do any one of the below:

<%= link_to 'Resend Pin', phone_numbers_resend_path, method: :post %>

                        or

get 'phone_numbers/resend' => "phone_numbers#resend"

Upvotes: 0

marmeladze
marmeladze

Reputation: 6572

link_to "Send Pin", phone_numbers_resend_path, method: :post

Upvotes: 1

Related Questions