小编Bre*_*ers的帖子

将JavaScript显示添加到主页以从140个字符开始倒计时.(Rails教程,第2版,第10章,练习7)

这个练习有点棘手.想我会发布我的解决方案,看看有没有人做过不同的事情,或者是否有人知道更好的方式.

我不确定使用Asset Pipline的最佳实践..例如,将内容放入application.js清单文件中的正确顺序,或何时将内容放入lib与app中.我只是在lib中添加以下内容以尝试使其工作.


来自Michael Hartl的Rails教程第2 章第10章,练习7:

(具有挑战性)将JavaScript显示添加到主页以从140个字符开始倒计时.

首先,我读了这篇关于jQuery类似Twitter的倒计时的帖子,它提供了执行此操作的代码.

接下来,我更新了app/views/shared/_micropost_form.html.erb,如下所示:

<%= form_for(@micropost) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <div class="field">
        <%= f.text_area :content, placeholder: "Compose new micropost..." %>
    </div>
    <%= f.submit "Post", class: "btn btn-large btn-primary" %>
    <span class="countdown"></span>

<% end %>
Run Code Online (Sandbox Code Playgroud)

然后,我在lib中创建了一个javascripts目录并添加了文件

LIB /资产/ JavaScript的/ microposts.js

function updateCountdown() {
    // 140 is the max message length
    var remaining = 140 - jQuery('#micropost_content').val().length;
    jQuery('.countdown').text(remaining + ' characters remaining');
}

jQuery(document).ready(function($) {
    updateCountdown();
    $('#micropost_content').change(updateCountdown);
    $('#micropost_content').keyup(updateCountdown);
}); …
Run Code Online (Sandbox Code Playgroud)

jquery ruby-on-rails-3 railstutorial.org asset-pipeline

13
推荐指数
1
解决办法
2787
查看次数

在Relationship模型中添加依赖:destroy的测试(第11章,练习1 Rails教程,第2版)

很确定这些测试工作正常.通过删除has_many:relationships上的dependent :: destroy选项以及user.rb中的has_many:reverse_relationships来解决它们.

想要分享我所做的事情以防其他人通过Michael Hartl的Rails教程第2版,第11章练习.

这个练习产生了一些问题(见本文的底部).如果有人能提供帮助,那就太好了.

第11章,练习1:

按照清单10.15中的示例,在关系模型(清单11.4和清单11.16)中添加依赖关系:destroy的测试.

这是我的测试: spec/models/user_spec.rb

require 'spec_helper'

describe User do

  before do
  @user = User.new(name: "Example User", email: "user@example.com", 
                   password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  [...code omitted...]

  describe "relationship associations" do
    let(:other_user) { FactoryGirl.create(:user) }
    before do
      @user.save
      @user.follow!(other_user)
      other_user.follow!(@user)
    end

    it "should destroy associated relationships" do
      relationships = @user.relationships
      @user.destroy
      relationships.should be_empty
    end

    it "should destroy associated reverse relationships" do
      reverse_relationships = @user.reverse_relationships
      @user.destroy
      reverse_relationships.should be_empty
    end
  end
Run Code Online (Sandbox Code Playgroud)

这个练习产生了几个问题:

问题1: …

rails-console ruby-on-rails-3 railstutorial.org dependent-destroy

7
推荐指数
1
解决办法
3257
查看次数

PGError:错误:关系"用户"不存在 - Rails教程第2版第9章Heroku

在使用rake任务填充数据库时,Hartl的Rails教程(第2版)第9章末尾遇到了错误.结束了它,但不知道出了什么问题.如果有其他人遇到这个错误,这就是我所做的.如果有人知道出了什么问题,请发表评论 - 我很想知道.谢谢!

跑这些命令

$ git push heroku
$ heroku run rake db:migrate
$ heroku pg:reset SHARED_DATABASE --confirm <name-heroku-gave-to-your-app>
$ heroku run rake db:populate
Run Code Online (Sandbox Code Playgroud)

然后,得到了这个错误:

rake aborted!
PGError: ERROR:  relation "users" does not exist
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"users"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Tasks: TOP => db:populate
(See full trace by running task …
Run Code Online (Sandbox Code Playgroud)

heroku pg railstutorial.org ruby-on-rails-3.2

3
推荐指数
2
解决办法
6667
查看次数

Rails 3嵌套Partials,Collections,Locals(Rails教程第2版:第10章,练习5)

通过Michael Hartl的Rails教程第2章第10章练习5,我遇到了部分和集合的问题,并在部分内部使用了部分.

第10章,练习5指出: "使用partials,消除清单10.46和清单10.47中删除链接中的重复."

代码清单10.46:app/views/microposts/_micropost.html.erb

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>
  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method:  :delete,
                                     confirm: "You sure?",
                                     title:   micropost.content %>
  <% end %>
</li>
Run Code Online (Sandbox Code Playgroud)

代码清单10.47:app/views/shared/_feed_item.html.erb

<li id="<%= feed_item.id %>">
  <%= link_to gravatar_for(feed_item.user), feed_item.user %>
    <span class="user">
      <%= link_to feed_item.user.name, feed_item.user %>
    </span>
    <span class="content"><%= feed_item.content %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
    </span>
  <% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method:  :delete, …
Run Code Online (Sandbox Code Playgroud)

collections partials ruby-on-rails-3 railstutorial.org

2
推荐指数
1
解决办法
2665
查看次数