小编pro*_*int的帖子

仅在属性存在时验证属性(仅当用户填写时)

如果它们不是空的,我只需要验证一些属性.

例如,用户可以具有徽标.如果我们尝试加载它 - 验证应该有效.如果我们只是更新没有徽标的用户数据,则必须跳过验证.

我现在有:

表单可以选择两个文件.一个 - 徽标,第二个 - 头像.这两个属性都是User模型的一部分.在用户模型中,有一个验证:

validates_preference_of :logo_file_name, :message=>I18n.t("...")
validates_format_of :logo_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("...")
validates_preference_of :avatar_file_name, :message=>I18n.t("...")
validates_format_of :avatar_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("...")
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如果我们尝试创建没有选择徽标和头像的新用户,我们将会出现错误(我们的验证).我尝试了更改验证并添加":on =>:update",如下所示:

validates_preference_of :logo_file_name, :message=>I18n.t("..."), :on => :update
validates_format_of :logo_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("..."), :on => :update
validates_preference_of :avatar_file_name, :message=>I18n.t("..."), :on => :update
validates_format_of :avatar_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("..."), :on => :update
Run Code Online (Sandbox Code Playgroud)

现在我可以创建没有选择徽标和头像的用户,但如果我尝试编辑用户并尝试上传只有徽标 - 我有头像的验证错误.如果我选择文件的头像和徽标留空 - 我有徽标验证错误.

我如何运行验证ony我想要更改的属性?

ruby validation ruby-on-rails

60
推荐指数
3
解决办法
3万
查看次数

如何在视图中启用Rails I18n翻译错误?

我创建了新的Rails 3项目.我尝试在我的视图中使用这样的翻译:

= t('.translate_test')
Run Code Online (Sandbox Code Playgroud)

在我的浏览器我看起来"translate_test"不是"my test translation"巫婆我在设置en.yml.

我的主要问题 - 为什么我看不到错误"Missing translation: en ..."

translation ruby-on-rails internationalization

6
推荐指数
2
解决办法
3410
查看次数

Capybara我怎么能填写所有相同名称的输入?

我有一个包含input可以动态添加或删除的字段的表单.我所有的投入都是

name='first_name[]'
Run Code Online (Sandbox Code Playgroud)

我如何填写我的所有投入?

rspec ruby-on-rails capybara

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

如何为elasticsearch用户设置密码?

我正在尝试为我的 ES 云启用安全性。我正在遵循以下说明:

https://www.elastic.co/guide/en/elasticsearch/reference/7.5/configuring-security.html

我被困在第 6 点。当我尝试运行时bin/elasticsearch-setup-passwords interactive出现错误:

Failed to determine the health of the cluster running at http://XXX:9200
Unexpected response code [503] from calling GET http://XXX:9200/_cluster/health?pretty
Cause: master_not_discovered_exception

It is recommended that you resolve the issues with your cluster before running elasticsearch-setup-passwords.
It is very likely that the password changes will fail when run against an unhealthy cluster.

Do you want to continue with the password setup process [y/N]
Run Code Online (Sandbox Code Playgroud)

我尝试检查我的 ES 实例状态:

curl http://XXX:9200/_cluster/health?pretty
Run Code Online (Sandbox Code Playgroud)

并得到

{
  "error" …
Run Code Online (Sandbox Code Playgroud)

elasticsearch

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

模块AbstractController :: Rendering的未定义方法render_to_body

我创建了一个新的rails应用程序(3.2.3,ruby 1.9)并尝试启动服务器.服务器正常启动,但后来我去了http:// localhost:3000我有错误:

    => Rails 3.2.3 application starting in development on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    [2012-05-09 22:20:03] INFO  WEBrick 1.3.1
    [2012-05-09 22:20:03] INFO  ruby 1.9.3 (2012-02-16) [i686-linux]
    [2012-05-09 22:20:03] INFO  WEBrick::HTTPServer#start: pid=3998 port=3000
    [2012-05-09 22:20:05] ERROR NameError: undefined method `render_to_body' for module `AbstractController::Rendering'
Run Code Online (Sandbox Code Playgroud)

这是什么?

ruby ruby-on-rails

3
推荐指数
1
解决办法
952
查看次数

如何在方法中存根类变量?

需要帮助;)这是代码示例:

def some_method
  @some_variable = "some logic for a few strings and operations"
end
Run Code Online (Sandbox Code Playgroud)

我想像这样存根这个方法

controller.stub(:some_method).and_return( ??? )
Run Code Online (Sandbox Code Playgroud)

但 @some_variable 也应该被定义,我该怎么做?

rspec ruby-on-rails

3
推荐指数
1
解决办法
8450
查看次数

Rails 3 - url中的友好参数(GET)

我有一个rails 3应用程序,现在我为我的目录实现过滤器.过滤器通过GET请求将数据传递给控制器​​.因此,在我提交表单(应用搜索)后,我在浏览器中有这样的链接:

http://localhost:3001/shoes?filter%5BShoeBottomType%5D%5B%5D=2&filter%5BShoeClassification%5D%5B%5D=1&filter%5BShoeClassification%5D%5B%5D=2&filter%5BShoeElation%5D%5B%5D=3&filter%5BShoeElation%5D%5B%5D=4&filter%5BShoeElation%5D%5B%5D=5&filter%5BShoeLiningColor%5D%5B%5D=2&filter%5BShoeLiningColor%5D%5B%5D=3&filter%5BShoeLiningColor%5D%5B%5D=4&filter%5BShoeTopColor%5D%5B%5D=1&filter%5BShoeTopColor%5D%5B%5D=2&filter%5Bonly_action%5D%5B%5D=1&page=2
Run Code Online (Sandbox Code Playgroud)

有没有办法让URL更漂亮?

PS我不想使用POST请求,因为我读到它对SEO不好

url ruby-on-rails

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