ben

OMÜ , Bilgisayar Mühendisliği, 13'

17 Kasım 2015 Salı

in-place edit


Show sayfası içerisinde alanlarımızı güncellemek için kullandığımız gem, best_in_place
modelimiz: rails g scaffold Post name:string  city:string cins:boolean adres:text birthday:date


gem file:

gem 'best_in_place'

application.js

//= require jquery
//= require best_in_place
//= require best_in_place.purr
//= require jquery_ujs
//= require jquery.purr
//= require bootstrap
//= require turbolinks
//= require_tree


jQuery ->
  $('.best_in_place').best_in_place()

posts/show.html.erb

text:

before;

<p>
  <b>Name:</b>
  <%= @post.name %>
</p>


after:
<p>
  <b>Name:</b>
  <%= best_in_place @post, :name %>
</p>


select:
<p>
  <b>City:</b>
  <%= best_in_place @post, :city, type: :select, collection: [["Çanakkale", "Çanakkale"], ["Konya", "Konya"] ,["Samsun","Samsun"]] %>
</p>

checkbox
<p>
  <b>Cins:</b>
  <%= best_in_place @post, :cins, as: :checkbox, collection: {false: "Kız", true: "Erkek"} %>
</p>

textarea

       post.rb
        
        def adres_format
             self.adres.gsub(/\n/, "<br>")
        end

<p>
  <b>Adres:</b>
      <%= best_in_place @post, :adres, :as => :textarea, :ok_button => 'Save', :cancel_button => 'Cancel',:display_as => 'adres_format' %>
</p>


date

<p>
  <b>birthdate:</b>
    <%= best_in_place @post, :birthdate, :display_with => lambda { |v| v.strftime("%Y-%m-%d") } %>
</p>



post_controller.rb

  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { respond_with_bip(@post) }
      else
        format.html { render action: "edit" }
           format.json { respond_with_bip(@post) }
       end
    end
  end


index sayfamız;



3 nolu kayıdımızın show sayfası

 

Tex edit:



select edit


textarea edit: