Я закончил Майкл Hartl в «Рубин на учебники» книгу и теперь я пытался добавить комментарии к microposts, но когда я попытался отправить комментарии, у меня есть ошибку:маршрутизации ошибка при добавлении комментариев в моем приложении
No route matches [POST]
Не могу найти там что там не так?
А также я хочу знать, что мои ассоциации верны между моделями? (Я хочу, чтобы создавать комментарии, как Facebook комментарии должностям)
comments_controller
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
user = User.find_by(params[:id])
micropost = user.microposts.build(params[:micropost])
@comment = Comment.new(params[:comment])
@comment.micropost_id = micropost_id
@comment.user = current_user
if @comment.save
flash[:success] = "Comment created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
end
comment.html.erb
<h5>Comments<h5>
<div class="comment">
<% @user.comments.each do |comment| %>
<p><%= comment.comment %></p>
<%= time_ago_in_words(comment.created_at) %> ago.
<%end%>
</div>
_comment_form.html.erb
<%= form_for :comment do |f| %>
<%= f.text_area :comment_comment, :size => "40x5", placeholder: "Comment..." %>
<%= f.submit "Post", class: "btn btn-primary" %>
<span class="picture">
<%= f.file_field :picture, accept:'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
_micropost. html.erb
<li id="micropost-<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
<span class="content">
<%= micropost.content %>
<%= image_tag micropost.picture.url if micropost.picture? %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</span>
<ol>
<%= render "comments/comment" %>
</ol>
<%= render 'shared/comment_form', micropost: micropost %>
</li>
create_comment.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :comment
t.references :user, index: true, foreign_key: true
t.references :micropost, index: true, foreign_key: true
t.timestamps null: false
end
add_index :comments, [:user_id, :micropost_id, :created_at]
end
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :micropost
end
micropost.rb
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments
end
user.rb
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :comments
end
route.rb
resources :microposts, only: [:create, :destroy] do
member do
resources :comments, only: [:create, :destroy]
end
end
Спасибо за вашу ссылку @Muhamad Акбар Bin Wida ... Попробую тогда, я буду обратно к вам. – Takor
Добро пожаловать. Удачи! – akbarbin