我想在“群组留言展示”视图中的帖子上设置评论表单。但是我有一条错误消息,例如
/ group_messages / 64的NoMethodError #<#:0x007f9c0afa0b38>
的未定义方法`group_message_group_message_comments_path' 你可以给我一些建议吗?☆show.html.erb(group_messages)
<h2>Add a comment</h2>
<%= form_for([@group_message, @group_message_comment]) do |f| %>
<% if @group_message_comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@group_message_comment.errors.count, "error") %> prohibited this group_message_comment from being saved:</h2>
<ul>
<% @group_message_comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %><%# do%>
</ul>
</div><!-- error_explanation-->
<% end %><%# if any?%>
<div class="field">
<%= f.label :member_id %><br />
<%= f.number_field :member_id %>
</div>
<div class="field">
<%= f.label :gmessage_id %><br />
<%= f.number_field :group_message_id %>
</div>
<div class="field">
<%= f.label :group_id %><br />
<%= f.number_field :group_id %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions" >
<%= f.submit "Comment"%>
</div>
<% end %><%# form_for>
☆group_messages_controller
def show
if !checklogin? then return end
@group_message = GroupMessage.find(params[:id])
@isme = me? @group_message
@group_message_comment = GroupMessageComment.new
@group_message_comment = GroupMessage.find(params[:id]).group_message_comments.build
respond_to do |format|
format.html # show.html.erb
format.json { render json: @group_message }
end
end
☆GroupMessage模型
class GroupMessage < ActiveRecord::Base
attr_accessible :content, :member_id, :group_id
belongs_to :member
belongs_to :group
has_many :group_message_comments
end
☆GroupMessageComment模型
class GroupMessageComment < ActiveRecord::Base
attr_accessible :content, :gmessage_id, :group_id, :member_id
belongs_to :member
belongs_to :group_message
end
☆routes.rb中
MiniSNS::Application.routes.draw do
resources :group_message_comments
root :to => 'members#login'
match '/groups/join'
resources :group_messages
resources :groups do
resources :group_messages
end
match '/members/new'
resources :index
resources :groups
post 'groups/:id' => 'group#show'
post '/groups/new'
post '/index/index'
match '/members/login'
match '/members/logout'
match '/members/friend'
match 'members/show'
post 'messages/comment'
resources :comments
resources :messages
resources :friends
resources :members
答案 0 :(得分:1)
该错误消息是由以下代码行引起的:
<%= form_for([@group_message, @group_message_comment]) do |f| %>
这是因为您没有为模型设置正确的路由。你可以发布你的routes.rb以便我可以看看吗?
我认为您需要嵌套路线,例如:
resources :group_messages do
resources :group_message_comment
end