从 Rails 中的不同页面提交消息
我有一个应用程序,允许用户在他们的个人资料上提交评论(想想 Facebook 的墙)。
截至目前,用户只能从主页提交评论,这会创建新的“评论”以及活动流的新“事件”。然后通过 AJAX 更新活动流。
但是,我希望用户也能够直接从他们的个人资料页面提交新评论。有没有办法让 Rails 检测它所在的页面?主页(控制器:home,操作:index)或个人资料页面(控制器:user,操作:show)并根据情况采取适当的ajax操作?这是我的代码:
对于评论表单:
<% form_remote_for Comment.new do |f| %>
<%= f.text_area :message, :value => "create a new comment...", :onfocus => "this.value=''" %>
<%= f.submit "post comment", :disable_with => 'posting', :class => 'button' %>
<% end %>
和我的评论控制器:
def create
comment = current_user.comment.create(params[:comment])
comment.save!
event = comment.user.events.create
event.kind = "comment"
event.data = { "message" => "#{comment.message}" }
event.save!
@user_stream = current_user.stream.paginate :page => params[:page], :order => "created_at desc"
render :update do |page|
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
end
end
我希望个人资料页面的评论发布是相同的,除了ajax:
render :update do |page|
page.insert_html :top, "profile", :partial => "home/new_comment", :locals => { :comment => comment }
page["comment_message"].clear
end
我可以使用rails来确定这个新评论帖子的来源吗,并适当地服务ajax?
我在想类似的事情:
render :update do |page|
if page['stream']
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
else
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
end
end
或者
render :update do |page|
if page.select('ul#stream').any?
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
else
page.insert_html :top, "comment_list", :partial => "home/new_comment", :locals => { :comment => comment }
page["comment_message"].clear
end
end
我也尝试过在个人资料评论、表单以及 request.request_uri 中使用隐藏字段标记,但都没有成功。我不想在评论模型中添加另一个元素来区分来源地。
但这些似乎都不起作用,所以我不知道该怎么办。
谢谢!
I've got an app that allows users to submit comments on their profiles (think facebooks wall).
As of right now, the users can only submit comments from the homepage, which creates a new "comment", as well as a new "event" for the activity stream. The activity stream is then updated via AJAX.
However, I want users to be able to submit a new comment directly from their profile page as well. Is there a way to have rails detect which page it is on? The home page (which is controller: home, action: index) or the profile page (controller: user, action: show) and take the appropriate ajax action depending? Here is my code:
for the comment form:
<% form_remote_for Comment.new do |f| %>
<%= f.text_area :message, :value => "create a new comment...", :onfocus => "this.value=''" %>
<%= f.submit "post comment", :disable_with => 'posting', :class => 'button' %>
<% end %>
and my comment controller:
def create
comment = current_user.comment.create(params[:comment])
comment.save!
event = comment.user.events.create
event.kind = "comment"
event.data = { "message" => "#{comment.message}" }
event.save!
@user_stream = current_user.stream.paginate :page => params[:page], :order => "created_at desc"
render :update do |page|
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
end
end
I want the comment posting from the profile page to be identical, except for the ajax will be:
render :update do |page|
page.insert_html :top, "profile", :partial => "home/new_comment", :locals => { :comment => comment }
page["comment_message"].clear
end
Can I use rails to determine where this new comment post is coming from, and serve the ajax appropriately?
I'm thinking something like:
render :update do |page|
if page['stream']
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
else
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
end
end
or
render :update do |page|
if page.select('ul#stream').any?
page.insert_html :top, "stream", :partial => "home/new_comment_event", :locals => { :comment => comment }
page["comment_message"].clear
else
page.insert_html :top, "comment_list", :partial => "home/new_comment", :locals => { :comment => comment }
page["comment_message"].clear
end
end
I've also tried using a hidden field tag in just the profile comments, form as well as request.request_uri and neither have worked. I'd rather not add another element to the comment model to differentiate between place of origin.
But neither of those seem to be working, so I'm at a loss for what to do.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用以下命令确定控制器中请求的来源
You should be able to determine the source of the request in the controller using
是否存在与其来源相关的不同行为?从上面的示例来看,它似乎在 if 和 else 块中执行完全相同的操作。
话虽如此,您没有理由不能传递另一个变量来确定它来自哪里,例如 params[:from] ,然后在渲染部分时检查它。
Is there different behavior associated with where it comes from? From your example above, it seems like it does exactly the same thing in both the if and else blocks.
That being said, there's no reason you can't pass another variable along for the ride to determine where it's coming from such as
params[:from]
and then check for that when rendering your partial.