Created
April 7, 2015 08:30
-
-
Save bergerjac/27d537e6de2823b8b70f to your computer and use it in GitHub Desktop.
rails async javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rendered in new.js.erb | |
<%= simple_form_for @task, remote: true do |f| %> | |
<%= f.input :description %> | |
<%= f.input :deadline %> | |
<%= f.button :submit %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= link_to edit_task_path(task), remote: true do %> | |
<button>Edit</button> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# update task list; hide form | |
$('#tasks').html("<%= j (render @tasks) %>"); | |
$('#task-form').slideUp(350); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (see rai-jax) | |
# `remote: true` disables Rails default of navigating to `/tasks/new` | |
<%= link_to new_task_path, remote: true do %> | |
<button>New</button> | |
<% end %> | |
<div id="task-form" style="display:none;"></div> | |
<div id="tasks"><%= render @tasks %></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#task-form').html("<%= j (render 'form') %>"); | |
$('#task-form').slideDown(350); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TasksController < ApplicationController | |
before_action :all_tasks, only: [:index, :create, :update] | |
before_action :set_tasks, only: [:edit, :update] | |
respond_to :html, :js | |
# index action rendered automatically | |
def new | |
@task = Task.new | |
end | |
def create | |
@task = Task.create(task_params) | |
end | |
def update | |
@task.update_attributes(task_params) | |
end | |
private | |
def all_tasks | |
@tasks = Task.all | |
end | |
def task_params | |
params.require(:task).permit(:description, :deadline) | |
end | |
edn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment