Skip to content

Instantly share code, notes, and snippets.

@vpamulap
Created February 6, 2012 20:18
Show Gist options
  • Save vpamulap/1754562 to your computer and use it in GitHub Desktop.
Save vpamulap/1754562 to your computer and use it in GitHub Desktop.
This is the code used to replace a recipe on a page using ajax.
______________________________________________
Link to Action in View:
______________________________________________
<%= link_to image_tag("refreshIcon.png"), replace_meal_queue_recipe_path(meal_queue_recipe), method: :post, remote: true %>
______________________________________________
Controller:
______________________________________________
class MealQueueRecipesController < ApplicationController
# GET /meal_queue_recipes
# GET /meal_queue_recipes.json
def replace
@mqr = MealQueueRecipe.find(params[:id])
@old_mqr_id = @mqr.id
# Save history
current_user.history.set_status(@mqr, "replaced")
@mqr.random_replace_recipe
@mqr.save
respond_to do |format|
format.html { redirect_to home_url }
format.js
end
end
...
end
______________________________________________
View returned by controller (replace.js.erb):
______________________________________________
// Swap out old meal queue recipe for the new one
$('#meal_queue_recipe_<%= @old_mqr_id %>').replaceWith('<%=j render @mqr %>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment