darkginger
darkginger

Reputation: 690

"No route matches [POST]" despite Resources in my Routes file

I am creating a Rails app, and I need a form to function in one of my views and submit data to a table without the use of a scaffold (like I usually do).

Now, the place where this comment form is going to appear is in one view within the blog folder. It will need to allow the user to put in their comment, save it to the table, and then return to the same page.

While this is a pretty commonplace error, I am confused because I am specifying two things that seem critical: creating resources in my routes file for the form, and second, using a create method in my controller.

In the blog.html.erb, this happens in this form:

<%= form_for :cements do |f| %>
            <div class="form-group">
                <div class="field">
                    <%= f.label :post %><br>
                    <%= f.text_area :post, class: "form-control" %>
                </div>
            </div>

            <h5 id="username">Username</h5>
            <div class="form-group">
                <div class="field">
                    <%= f.text_field :username, class: "form-control" %>
                </div>
            </div>

            <%= f.hidden_field :slug, :id => "hiddenPicker"%>

    <div class="actions">
        <%= f.submit "Save", class: "btn btn-success-outline" %>
    </div>
<% end %>

Then, in my controller, I have a create method that should redirect back to the original page, as I wanted.

blogs_controller.rb

class BlogsController < ActionController::Base

    def index
        @posts = Post.order('updated_at DESC').all
        @comments = Cement.all
    end

    def blog
        @posts = Post.where(slug: params[:id]).all
        @comments = Cement.all
    end 

    def create
        @cements= Cement.new(story_params)
        @cements.save
        redirect_to(:back)
    end

    private

    def story_params
        params.require(:cements).permit(:username, :post, :slug)
    end

end

Good news: the comment form renders in the view. Bad news: when I submit, I am getting this error: No route matches [POST] "/blog".

My expectation is this will be an issue with my Routes file; however, I have a resources method already in there:

Rails.application.routes.draw do resources :posts resources :cements resources :blogs

The naming convention is the same as my controller file, so I am confused why this error is happening. Any ideas?

Upvotes: 0

Views: 338

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 3427

:cement is not an object it is just a symbol, so how rails will determine where to POST form? If you inspect your form you will see form action as /blog (current page url).

You should either do

<%= form_for :cements, url: cements_path do |f| %>

or

<%= form_for Cement.new do |f| %>

Both of above will generate form action as /cements, which will submit to CementsController create action, But I see in your case you want to submit it to BlogsController so use the appropriate routes(blogs_path). You can use url in second version also.

Upvotes: 1

Related Questions