我是ROR的新手,我喜欢它,因为我开发了我的第一个应用程序.当我将格式应用于导航菜单时,我有一个与我的应用程序模板相关的问题.
是否可以检查url路径是否与root:to config.rb中设置的路径匹配?我有一个辅助方法,它返回字符串"current",它添加了css类样式以突出显示所选的菜单项.只要我不在主页,帮助方法就可以正常工作.当我的网址是www.localhost:3000/css当前类没有应用于Products链接,因为request_uri ="/"不等于"/ products".当我在主页上时,我希望将css类"current"应用于Products菜单项.
是否有任何条件逻辑可用于获取根:路径并检查它是否与is_current的参数路径匹配?
这是我的代码:
root:to =>'products #index'
def is_current(path)
if request.request_uri == path
return 'current'
end
end
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
谢谢,bkasen
有人可以帮助选择帮手,因为我是Ruby On rails的新手.
我有一个json对象如下,
@response = {"status" => "200", "fruit" => [ {
"id" => 1,
"name" => "Apple"
}, {
"id" => 2,
"name" => "Mango"
} ]
}
Run Code Online (Sandbox Code Playgroud)
在帮助器中我返回值"return @response ['rateCard']"现在我想从这个帮助器在视图文件中生成这样的代码所以它将有选择框像
<select name='fruit'>
<option value="1">apple</option>
<option value="2" selected>mango</option>
</select>
Run Code Online (Sandbox Code Playgroud)
实际上我有一个帮助者"Fruits_helper.rb"正在返回
def selFruit
@resp = Fruit.getFruit # this will return @response object as mentioned above
return @resp['fruit']
end
Run Code Online (Sandbox Code Playgroud)
================================================== ================================所以在我的fruit.html.erb文件中我有一小段代码如下
<%= form_for(:AdminLogin) do |f| %>
<div>
<%= select_tag "fruit", options_for_select(selFruit.each{|fruit,key| [fruit[key]=>'name',fruit[key]=>'id']}) %>
<div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
================================================== ==========================上面的代码给了我o/p …
我有一个简单的foreach模板,在每个元素里面我想要一个ActionLink,但ActionLink需要发送一个Id来编辑元素.
要模板化的项目:
<div data-bind="template: {
name: 'postsTemplate',
foreach: posts
}">
</div>
Run Code Online (Sandbox Code Playgroud)
模板:
<script id="postsTemplate" type="text/html">
<h2 data-bind="text: Title"></h2>
<p class="post-info">
<p class="post-info" data-bind="text UserName"></p>
<span data-bind="Body"></span>
<p class="post-footer">
@Html.ActionLink("Comments", "IndividualPost", "Post", null, null, "comments", new {id = })
</p>
</p>
</script>
Run Code Online (Sandbox Code Playgroud)
如何Id通过ActionLink 发送实际帖子?我的意思是,如何在不使用数据绑定的情况下访问帖子的ID?(因为它是帮助者).
我正在尝试创建一个日历表来显示公司每个员工的每日可用性.我认为我需要的是一个标题,其中包含每月的日期,然后是每个工作人员的一行(具有与天数一样多的单元格).每个单元格的不同背景颜色将代表不同的可用性状态,并且工作人员会更改,因此我希望动态生成所有这些.
经过网上研究并以Ryan Bates的Railscast#213(日历修订版)作为我的向导,我到目前为止设法生成了我的表格的标题,显示了每个单元格中每月的日期.
到目前为止,这是我的代码:
module ReceptionHelper
def reception(date = Date.today, &block)
Reception.new(self, date, block).table
end
class Reception < Struct.new(:view, :date, :callback)
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
daysHeader
end
end
def daysHeader
last_day = date.end_of_month.strftime("%d").to_i
(0..last_day).to_a.map { |day| content_tag :th, day }.join.html_safe
end
end
Run Code Online (Sandbox Code Playgroud)
现在,问题和困惑开始于此:
正如我所发现的那样,删除&block并删除:callback{...}功能.我不能说我理解为什么.谁有一个很好的解释?
为什么以上工作,但我不能使用(..).each do和content_tag下面的块?
(0..last_day).to_a.each do |day|
content_tag :th do
day
end
end
Run Code Online (Sandbox Code Playgroud)在我努力为每个员工显示行时,我选择了这个:
def table
content_tag :table, class: "calendar" do …Run Code Online (Sandbox Code Playgroud)我需要一个帮助器,它生成一个包含在<li>包含活动类的链接.
没有支持块这很容易:
def nav_item(*args, &block)
url = args[1]
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
link_to(*args)
end
end
Run Code Online (Sandbox Code Playgroud)
但就像link_to我希望我的助手支持用于定义内容的块一样.使用link_to,我可以这样做:
那么如何在我的助手中支持相同的内容呢?
我需要做的就是将块传递给link_to.我目前的尝试
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block_given?
# What goes here?
else
link_to(*args)
end
end
end
Run Code Online (Sandbox Code Playgroud) 正如你们可能已经猜到的那样,这是一个面试问题.但我不会透露哪家公司.我被要求实施
1.day.ago
Run Code Online (Sandbox Code Playgroud)
在Ruby中.这是Rails中的日期帮助器,但Ruby中不存在此功能.
我有
资源:博客
在我的routes.rb文件中声明但是当我尝试访问控制器或html.erb文件中的blog_path时,我收到以下错误:
没有路由匹配{:controller =>"blog",:action =>"show"}缺少必需的密钥:[:id]
我创建了一个名为BlogController的控制器,并在views目录中使用show.html.erb文件定义了方法show.如果我定义:
匹配'/ blog',到:'博客#show',通过:'get'代替,然后blog_path工作正常.
我的理解是资源:博客只是匹配'/博客'的语法糖,:'博客#show',via:'get'和一堆其他路线.请帮忙.
所以我有一个codeigniter帮助程序,用于确定已签名的请求是否来自移动.因此,当有移动请求时,我希望我的url路由到不同的控制器.
例如
if (get_request() === 'mobile')
$route['u/a/(:any)'] = "mobile/usr/main_controller/game_detail/$1";
else
$route['u/a/(:any)'] = "pc/usr/main_controller/game_detail/$1";
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我在spec/support/request_helpers.rb中添加了一些代码
module Requests
module JsonHelpers
def json
@json ||= JSON.parse(response.body)
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后我将配置行添加到spec/rails_helper.rb以从我的规范访问该代码.
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.include Requests::JsonHelpers, :type => :controller
end
Run Code Online (Sandbox Code Playgroud)
因此,当我从spec/controller/tasks_controller_spec.rb运行我的规范时,我收到以下错误.
/spec/rails_helper.rb:16:in `block in <top (required)>': uninitialized constant Requests (NameError)
Run Code Online (Sandbox Code Playgroud)
怎么解决?
我的规格require 'rails_helper'在顶部.
这是一个非常简单的问题,但我无法找到我想要的东西:
在Scala的某个地方有帮助吗?
def stringOption(string: String): Option[String] =
if (string == null) None else Some(string)
Run Code Online (Sandbox Code Playgroud)
我无论在哪里使用Java库,我都会复制粘贴相同的代码.
如果实际是String,它只是将String转换为Option [String] null.Java中许多库中的许多方法都会在null无法返回任何内容时返回.
谢谢
helper ×10
ruby ×5
block ×2
routes ×2
asp.net-mvc ×1
codeigniter ×1
content-tag ×1
date ×1
java ×1
knockout.js ×1
link-to ×1
menu ×1
module ×1
navigation ×1
null ×1
php ×1
rspec ×1
scala ×1
select ×1
selecteditem ×1