Rails: unobtrusive checkin function
I have a simple checkin app: there's a page with a series of pictures of users. They are "checked in" by a simple click on the image. This registers them in the database and displays a "checked in" icon overlaying the image.
Right now I'm doing this by a "link_to" in the view which calls the register controller action. But this means I have to reload the entire page after every checkin. I want to do this without the reload, but I can't figure out how to do it. Maybe I need a different approach?
Here's the (simplified) controller action:
def register
@individual = @household.individuals.find(params[:id])
@individual.last_checkin = Time.now()
@indi开发者_如何学Govidual.save
redirect_to :back
end
Here's the view:
- @household.individuals.each do |individual|
.individual
= link_to image_tag(individual.picture.url(:large), :class => 'picture'), register_household_individual_path(@household, individual)
.checked-in
- if individual.checked_in?
= "Checked in today at #{individual.last_checkin.to_s(:time)}"
= image_tag('/images/checked_in.png')
%h2
%span
= individual.firstname + " " + individual.lastname
%br
= individual.relationship
(The image overlay is handled in CSS)
How would I do this with ajax? It's not a form submit so I can't use a .js.erb file. Maybe it should be, but I don't know how to go about it.
Any ideas?
Why not use Rail's :remote => true
on the link_to? http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
精彩评论