【发布时间】:2017-01-09 20:17:41
【问题描述】:
我正在尝试将我的评论表单呈现到除帖子视图之外的另一个视图中,但我收到了错误: 没有路由匹配 {:action=>"index", :controller=>"cmets", :post_id=>nil} 缺少必需的键:[:post_id]
我的模型:
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end
class Post < ApplicationRecord
belongs_to :user
has_many :comments
mount_uploader :image, AvatarUploader
validates :content, presence: true, length: {minimum: 5}
delegate :username, :username=, :email, :email=,:avatar, :avatar=, :to => :user, allow_nil: true
end
class Wall < ApplicationRecord
has_many :post
has_many :users
has_many :comments
delegate :username, :username=,:avatar, :avatar=, :email, :email=, :to => :user, allow_nil: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
我在墙控制器中呈现的表单 -> index.html.erb
<div class="comments">
<%= form_for ([@post, @post.comments.build]) do |f| %>
<%= f.text_area :content, class: 'comments js-auto-size', id: 'alex2' ,:rows => 1 %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
更新:
Rails.application.routes.draw do
devise_for :users
resources :uploads
resources :users
resources :walls
resources :posts do
resources :comments
end
root 'walls#index'
end
将此表单添加到帖子视图没有问题,但是我希望将此表单和 cmets 都呈现到墙上 index.html.erb
这个应用程序基本上就像一个 facebook,所以所有内容都将在 1 页上呈现。
【问题讨论】:
-
你能显示你的 routes.rb 吗?
-
添加路由到帖子
标签: ruby-on-rails ruby