Ruby on Rails & ldquo; The 'create' action was not found for MessagesController & rdquo;

advertisements

I have an issue with Ruby on Rails routing.

config/routes.rb:

Rails.application.routes.draw do

  resources :messages

  root to: 'dashboards#show'

end

app/controllers/messages_controller.rb:

class MessagesController < ApplicationController

    def index
        # Do current user messages select from database here
    end

end

link to messges:

<%= button_to 'messages', messages_path %>

URL:

http://localhost:3000/messages

Error:

The action 'create' could not be found for MessagesController

Why I have this issue? What am I doing wrong? Why I get this error?


The button_to method does a post by default. When you have specified the route as a resource, it maps a post to the #create method. You need to define that method, stop using a post, or modify your routes.rb to send a post to a different method. From the looks of how you are using it, you should probably just modify the button_to to use a get:

<%= button_to 'messages', messages_path, method: :get %>