Reputation: 233
Error appears when I try to submit the comment.
routes.rb file:
scope module: 'admin' do
resources :comments
end
_form
<%= form_for new_comment_path do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %>
...
<%= submit_tag 'Submit', :class => 'btn btn-primary' %>
<% end %>
comments_controller.rb
def new
@comment = Admin::Comment.new
end
def create
@comment = Admin::Comment.new(comment_params)
...
end
Upvotes: 0
Views: 849
Reputation: 53038
Use
<%= form_for(@comment, url: comments_path) do |f| %>
As you need to post the form, you need a POST route. This will automatically POST your form to create
action(comments_path
). Logically new
form should be posted to create
action of your controller.
new_comment_path
refers to GET
route of new page which you are trying to use on form. It is not a POST
route so you get the error.
new_comment GET /comments/new(.:format) admin/comments#new
POST /comments(.:format) admin/comments#create ## You need this route
Define your routes as below:
namespace :admin do
resources :comments
end
and you can use form_for
as
<%= form_for(@comment) do |f| %>
Upvotes: 2
Reputation: 76784
I think you'll have to use:
<%= form_for [:admin, @comment] do |f| %>
Here's a good reference: form_for and scopes, rails 3
Upvotes: 0