【发布时间】:2014-03-28 23:05:02
【问题描述】:
我有一个表单,允许用户在组 Show 方法中发布。发布后,我想重定向到显示新帖子的同一页面。我正在使用以下内容,但出现以下错误。我不确定为什么@group 是 nil,因为我已经在我的组控制器的显示中定义了它。
没有路由匹配 {:id=>nil} 缺少必需的键:[:id] 为了 重定向到组路径(@group)
<%=form_for([@post]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class = "field">
<%= f.label :event_name %>
<%= f.collection_select(:event_id, @events, :id, :title) %>
</div>
<div class = "field">
<%= f.text_area :comment, placeholder: "New Post..." %>
</div>
<%= f.hidden_field :user_id, value: current_user.id %>
<%=f.submit "Submit", class: "btn btn-large btn-primary" %>
<%end%>
class PostsController < ApplicationController
def create
if @post = Post.create(post_params)
flash[:success] = "Post Created!"
redirect_to group_path(@group)
else
redirect_to group_url
flash[:alert] = "Sorry - Post not created."
end
end
end
def show
@event = @group.events.build
@post = Post.new
@events = @group.events.includes(:posts)
@group = Group.find(params[:id])
end
【问题讨论】: