小编ben*_*min的帖子

如何构建一个具有has_many关系的模型表单

我有一个Phrasehas_many PhraseTranslation.

应用程序/模型/ phrase.rb

class Phrase < ActiveRecord::Base
  has_many :translatabilities
  has_many :translations, through: :translatabilities
  has_many :inverse_translatabilities, class_name: "Translatability", foreign_key: "translation_id"
  has_many :inverse_translations, through: :inverse_translatabilities, source: :phrase
  accepts_nested_attributes_for :translatabilities
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ phrases_controller.rb

class PhrasesController < ApplicationController
  def index
    @phrases = Phrase.all.page params[:page]
    @translations = @phrases.count.times.map do |i|
      translation = Phrase.new
      translation.translatabilities.build
      translation
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我想为每个"短语"添加"可译性"形式.

应用程序/视图/短语/ index.html.erb

<table>
  <tbody>
  <% @phrases.each do |phrase| %>
      <tr>
        <td><%= phrase.text %></td>
      </tr>
      <tr>
        <td>
          <%= form_for @translations …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many-through

6
推荐指数
1
解决办法
128
查看次数

推动Heroku失败 - 没有这样的应用致命

Git push on heroku指向一个不存在的git存储库.

git.heroku.com/secure-reef-1722.git这是我们运行heroku create命令时创建的存储库.

但是当我们运行'push'命令($ git push heroku master)时,它说

远程:!没有像沸腾入口6957这样的应用程序.致命:未找到存储库' https://git.heroku.com/boiling-inlet-6957.git/

当我们运行$ git remote -v时,我们也无法看到新的存储库

heroku  https://git.heroku.com/boiling-inlet-6957.git (fetch)
heroku  https://git.heroku.com/boiling-inlet-6957.git (push)
origin  git@bitbucket.org:coderz$/toy_app.git (fetch)
origin  git@bitbucket.org:coderz$/toy_app.git (push)
Run Code Online (Sandbox Code Playgroud)

现在我们无法将文件推送到新的heroku git存储库(git.heroku.com/secure-reef-1722.git)

请帮助我们.提前致谢.

完整的命令序列

coderz$:~/workspace/toy_app (master) $ heroku create
Creating secure-reef-1722... done, stack is cedar-14
https://secure-reef-1722.herokuapp.com/ | https://git.heroku.com/secure-reef-1722.git
coderz$:~/workspace/toy_app (master) $ git push heroku master
remote: !       No such app as boiling-inlet-6957.
fatal: repository 'https://git.heroku.com/boiling-inlet-6957.git/' not found
coderz$:~/workspace/toy_app (master) $ git remote -v
heroku …
Run Code Online (Sandbox Code Playgroud)

git heroku

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

测试驱动开发 - 单元测试(在CakePHP中)

我在CakePHP中进行单元测试时遇到了一些问题,特别是在测试数据库插入/更新时.假设我有一个类似这样的模型:

class User {
  var $name = 'User';

  function updatePassword($data) {
    return $this->updateAll($data);
  }
}

class UserTestCase {
  function testUpdatePassword() {
    $tmpData = array(
      'User' => array(
         'password' => sha1(uniqid('', true)) //dummy pass
    );

    $result = $this->User->updatePassword($tmpData);

    $this->assertTrue($result);
  }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是在我的测试用例中:

  • 我必须提供通常从表单中检索的虚拟数据
  • 虚拟数据的格式没有考虑到实际表单数据可能不正确的事实
  • 我只测试更新是否成功:创建所有虚拟数据来测试它似乎需要付出很多努力

这个例子似乎有点人为(我可以update在控制器中做一个没有创建额外模型方法的例子),但重点是,在测试更新/插入时,数据是虚拟数据,从表单检索的数据可能不同而且好处似乎并没有超过成本.

您对TDD和单元测试的方法表示赞赏,并且了解您通常会尝试为案例提供何种覆盖范围.

干杯

testing tdd unit-testing cakephp

4
推荐指数
1
解决办法
3247
查看次数