(我想做的)
使用表单,我想将一些数据保存到GroupMessage模型中,同时在我填写表单的页面上显示组消息(groups/show?id=)。
(我做了什么)
我编写了如下代码。按下 Create Group Message 按钮后,我进入页面,http://localhost:3000/groups/:id/group_messages
and got an error
"We're sorry, but something went wrong".
帮助!!
(组/节目?id=)
<%= form_for([@group, @group_message]) do |f| %>
<% if @group_message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@group_message.errors.count, "error") %> prohibited this group_message from being saved:</h2>
<ul>
<% @group_message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :member_id %><br />
<%= f.number_field :member_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_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
(groups_controller)
def show
@group = Group.find(params[:id])
@group_message = Group.find(params[:id]).group_messages.build
if !checklogin? then return end
groupfriends = GroupInMember.where(:member_id => session[:login].id)
gflist = '('
groupfriends.each do |gf|
gflist += gf.member_id.to_s + ","
end
gflist += session[:login].id.to_s + ')'
@query = "select * from group_messages where member_id in " + gflist
@group_messages = GroupMessage.find_by_sql(@query)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @group_messages }
end
end
(GroupMessage 表) create_table "group_messages", :force => true do |t| t.integer "member_id" t.text "content" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "group_id" end