arailsdemo 4

 添加一些 辅助(helper) 方法

app/helpers/layout_helper.rb

module LayoutHelper
  def title(page_title, show_title = true)
    content_for(:title) { page_title.to_s }
    @show_title = show_title
  end

  def show_title?
    @show_title
  end

  def stylesheet(*args)
    content_for(:head) { stylesheet_link_tag(*args) }
  end

  def javascript(*args)
    content_for(:head) { javascript_include_tag(*args) }
  end
end

修改 Application 布局(Layout)

app/views/layouts/application.html.haml

%html(lang="en")
%html(lang="en")
  %head
    ...
    %title
      = "aRailsDemo#{(" | " + yield(:title)) unless yield(:title).blank?}"
    ...
    = yield :head

在 Post 的表单(_form )使用 JS

app/views/posts/_form.html.haml

-javascript 'post'

= simple_form_for @post do |f|
    ...

写 Post Javascript

public/javascripts/post.js

$(function(){
  $('.section:last').after('<a href="#">Add another section<a/><br/>');

  $('a[href=#]').click(function(){
    $(this).before($('.section:last').clone());

    $('.section:last label').each(function(){
      var labelFor = $(this).attr('for');
      $(this).attr('for', labelFor + '1');
    })

    $('.section:last input, .section:last textarea, .section:last select').each(function(){
      var inputID = $(this).attr('id');
      var inputName = $(this).attr('name');      
      var splitName = inputName.split(/\]\[/);

      splitName[1] = parseInt(splitName[1], 10) + 1;
      $(this).attr('name', splitName.join("]["));
      $(this).attr('id', inputID + '1');

      if ($(this).is('input')){
        $(this).removeAttr("value");        
      } else if ($(this).is('textarea')) {
        $(this).html("");
      }
    });

    $('.section:last input[type=hidden]').remove();

    return false;
  });
})

你可能感兴趣的:(arailsdemo 4)