submitting messages from different pages in rails
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 =&开发者_高级运维gt; "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!
You should be able to determine the source of the request in the controller using
request.request_uri
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.
精彩评论