我与User
和Micropost
(User has_many Micropost and Micropost belongs_to User
)有关系。
在一个模态中,我有一个带有select2
框的表单。我想做的是,选择尽可能多的用户,并从单个表单向这些用户发布微博。
我该怎么做呢
基本上我想在一个表单提交的select2框中创建具有所选用户的user_id的微博。
发布后的Micropost表(假设我选择了4位用户发布我的消息)
micropost_id micropost user_id posted_by_user_id
----------------------------------------------------------------------
1 Some Message 1 Current_user_id(say 5)
2 Some Message 2 Current_user_id(say 5)
3 Some Message 3 Current_user_id(say 5)
4 Some Message 4 Current_user_id(say 5)
表格
<form accept-charset="UTF-8" class="simple_form new_share" id="new_share" method="post">
<%= csrf_meta_tag %>
<div class="form-inputs">
<select multiple id="UserSelect" style="width:100%">
<%@users.each do |user| %>
<option value="<%=user.id%>"><%=user.username%></option>
<% end %>
</select>
<div class="control-group text required">
<div class="controls">
<p> Enter your Custom Message </p>
<textarea class="text required span4" id="review_text_area"
name="review[review]" placeholder="Share"
rows="3">
</textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button id="ShareModalSubmit" class="btn" type="submit" value="Share">Share</button>
<button id="share-confirm-close" class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
</div>
我不知道,如果我有意义......
这甚至可能吗?如果不是我如何实现此功能?
整晚都在工作..
请耐心等待,我是个新人。
答案 0 :(得分:0)
我成功地解决了这个......不确定它是否是最干净的解决方案。 如果有人知道任何干净的解决方案,请告诉我。
def create
respond_to do |format|
format.js {
if params[:Users_ids] #Coming from select box
univ_id = params[:univ_id]
univ_name = params[:univ_name]
params[:SelectedUsers_ids].each do |user_id|
@user = User.find(user_id.to_i)
params[:user_id] = user_id.to_i
@micropost = @user.microposts.build(params[:micropost])
if @micropost.save
flash[:notice] = "We shared your message with the selected users"
else
flash[:error] = "Sorry! We could not share your message !"
end
end
return
else
flash[:error] = "Sorry! We could not share your message !"
redirect_to request.referer
return
end
}
format.html {
@user = User.find(params[:user_id].to_i)
@micropost = @user.microposts.build(params[:micropost])
if @micropost.save
flash[:notice] = "We shared your message successfully!"
redirect_to request.referer
else
flash[:error] = "Sorry! We could not share your message !"
redirect_to request.referer
end
}
end # end of respond to
end