Skip to content

Instantly share code, notes, and snippets.

@mdunbavan
Created September 23, 2014 14:26
Show Gist options
  • Select an option

  • Save mdunbavan/19c4488d653fe301eb9c to your computer and use it in GitHub Desktop.

Select an option

Save mdunbavan/19c4488d653fe301eb9c to your computer and use it in GitHub Desktop.
ajax submit
// Inside books/form
<%= simple_form_for(@book, :remote => true, :authenticity_token => true, :html => { :multipart => true }) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.file_field :jacket_cover %>
<%= f.input :title %>
<%= f.input :synopsis, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } } %>
<%= f.input :body, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } } %>
<%= f.input :age, collection: 0..100, prompt: "Select age range" %>
<%= f.input :publisher %>
</div>
<h4>Select cuurent authors if applicable: </h4>
<%= f.select( :author_id, Author.all.map {|u| [u.name,u.id]}, {:include_blank => false, prompt: "No Author"} ) %>
<div id="author-inputs" style="display: none !important;">
<h4>If you cannot see the author above create one here! </h4>
<%= render :partial => 'authors/form', :object => @author %>
</div>
<!-- Important bit here -->
<%= link_to 'New Author', new_author_path, :remote => true, :id => "new_author_link" %>
<div id="authors"></div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
def new
@author = Author.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @author }
format.js
end
end
def edit
end
def create
@author = Author.new(author_params)
respond_to do |format|
if @author.save
format.html { redirect_to @author, notice: 'Author was successfully created.' }
format.json { render action: 'show', status: :created, location: @author }
format.js
else
format.html { render action: 'new' }
format.json { render json: @author.errors, status: :unprocessable_entity }
format.js
end
end
end
<%= simple_form_for(@author, :remote => true) do |f| %>
<%= f.error_notification %>
<%= token_tag nil %>
<%= notice %>
<div class="inputs">
<%= f.input :name %>
<%= f.input :biography, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } } %>
<%= f.file_field :avatar %>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
// In authors/form
<% if @author.errors.count == 0 %>
$('#new_author').remove();
$('#new_author_link').show();
$('#authors-added h3').html('Author sucessfully added');
$('#authors').append('<%= j render("author", :author => @author) %>');
<% else %>
$('.new_author').remove();
$('#new_author_link').after('<%= j render("form") %>');
<% end %>
// In authors/form
$('#new_author_link').hide().after('<%= j render("form") %>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment