有时我看到Ruby中的方法有"?" 和"!" 在他们的最后,例如:
name = "sample_string"
name.reverse
name.reverse!
name.is_binary_data?
Run Code Online (Sandbox Code Playgroud)
我想知道他们的目的是什么?它们只是语法糖衣吗?
我想简单地检查表单文本字段中的返回值是否为数字,即:12,12.5或12.75.有没有一种简单的方法可以检查这一点,特别是如果将值拉为param?
什么是在rails 3中设置页面标题的正确方法.目前我正在做以下事情:
应用程序/视图/布局/ application.html:
<head>
<title><%= render_title %></title>
<%= csrf_meta_tag %>
Run Code Online (Sandbox Code Playgroud)
应用程序/佣工/ application_helper.rb:
def render_title
return @title if defined?(@title)
"Generic Page Title"
end
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/ some_controller.rb:
def show
@title = "some custom page title"
end
Run Code Online (Sandbox Code Playgroud)
有没有其他/更好的方法来做上述事情?
鉴于字符串:
"Hello there world"
Run Code Online (Sandbox Code Playgroud)
如何创建URL编码的字符串,如下所示:
"Hello%20there%20world"
Run Code Online (Sandbox Code Playgroud)
如果字符串也有其他符号,我也想知道该怎么做,比如:
"hello there: world, how are you"
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?我打算解析,然后为此构建一些代码.
rails 3似乎逃脱了一切,包括html.我尝试过使用raw()但它仍然逃脱了html.有解决方法吗?这是我正在使用的帮手(/helpers/application_helper.rb):
module ApplicationHelper
def good_time(status = true)
res = ""
if status == true
res << "Status is true, with a long message attached..."
else
res << "Status is false, with another long message"
end
end
end
Run Code Online (Sandbox Code Playgroud)
我使用以下代码在视图中调用帮助器:
<%= raw(good_time(true)) %>
Run Code Online (Sandbox Code Playgroud) 我用devise和漂亮的生成器创建了一个数据库.我正在尝试使用nifty generator(rails g nifty:scaffold Asset user_id:integer)创建一个新数据库,但是当我尝试迁移数据库(rake db:migrate)时,我收到以下错误:
charlotte-dator:showwwdown holgersindbaek$ rake db:migrate
== DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `email` varchar(255) DEFAULT '' NOT NULL, `encrypted_password` varchar(128) DEFAULT '' NOT NULL, `reset_password_token` varchar(255), `reset_password_sent_at` datetime, `remember_created_at` datetime, `sign_in_count` int(11) DEFAULT 0, `current_sign_in_at` datetime, `last_sign_in_at` datetime, `current_sign_in_ip` varchar(255), `last_sign_in_ip` varchar(255), `name` varchar(255), `created_at` datetime, …Run Code Online (Sandbox Code Playgroud) 在Rails 3中,可以做一些事情,例如some_post.comments.append(some_comment)某些帖子是"has_many"评论的模型实例.
我在Rails 4中面临的问题是该append方法现在保存到DB(像push和<<),我只需"追加"而不将附加对象保存到DB.
我们如何在Rails 4中实现这一目标?我无法使用,some_post.comments.build(some_comment.attributes)因为我需要保留some_comment实例中已存在的其他关系.
我无法在CSS的显示属性中找到任何说明默认显示相同的内容.我问,因为每当我尝试向<a>标签添加填充或边距时,它都不会添加它,我必须为其添加显示属性inline-block.
我不知道这是否是浏览器特定的,但它的默认显示是inline对比说inline-block(我显然知道它不是inline-block.
在Rails中创建所需字段的最简单方法是什么?
inquiry.rb:
class Inquiry < ActiveRecord::Base
attr_accessible :address, :email_id, :gender, :message, :mobile_number, :name
end
Run Code Online (Sandbox Code Playgroud) 我很想知道send和之间的区别public_send.例如:
class Klass
def hello(*args)
"Hello " + args.join(' ')
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
k.public_send :hello, "gentle", "readers" #=> "Hello gentle readers"
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这个区别吗?