Rails最好的宝石

时间:2012-07-17 15:26:30

标签: ruby-on-rails-3 jquery

我跟随Ryan Bates'railscast获得了最好的宝石,但无法让它正常工作。我在user.rb模型中设置的before_create的默认值显示在页面上但我无法点击它进行编辑;它只是作为一个静态元素显示。虽然,Firebug显示在视图<head>中加载了best_in_place javascript。

的application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require best_in_place
//= require_tree .

users.js.coffee

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

user.rb

attr_accessible :email, :name, :password, :password_confirmation, :avatar, :goal
...
before_create :default_values
...
  private

    def default_values
      self.goal ||= "Write Current Goal Here"
    end

users_controller.rb

respond_to :html, :json
...
  def update
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html {
          flash[:success] = "Profile updated"
          sign_in @user
          redirect_to @user
        }
        format.json { respond_with_bip(@user) }
      else
        format.html { render 'edit' }
        format.json { respond_with_bip(@user) }
      end
    end
  end

用户/ show.html.erb

...
<%= best_in_place @user, :goal %>

3 个答案:

答案 0 :(得分:2)

在你的layouts / application.html.erb ..而不是javascript_include_tag(:all)

javascript_include_tag(:application) 

另外,请确保users.js.coffee住在app/assets/javascript

修改

确保您已在Gemfile和bundle中指定了gem

gem "best_in_place"

试试这个,就像赛义德·卡尔巴耶夫所说的那样

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require_tree .
//= require best_in_place

然后在页面顶部执行此操作而不是咖啡:

$(document).ready(function() {
  $('.best_in_place').best_in_place();
})

如果不起作用,请删除//= require best_in_place//= require jquery.purr,然后运行

rails g best_in_place:setup

答案 1 :(得分:1)

我也有同样的问题,通过将需要best_in_place 移动到需求列表的末尾来解决

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require_tree .
//= require best_in_place

答案 2 :(得分:1)

这对我有用:

更改

$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});

$(document).ready(function() {