下面是我的index.html.erb(我只想显示品牌和相关子品牌列表)
<h1>Brands</h1>
<% @brands.each do |brand| %>
<h2><%= brand.name %></h2>
<% end %>
<% @brands.subbrands.each do |subbrand| %>
<h2><%= subbrand.name %></h2>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我在查看index.html时收到的错误是:
undefined method `subbrands' for #<Array:0x9e408b4>
Run Code Online (Sandbox Code Playgroud)
这是我的brands_controller:
class BrandsController < ApplicationController
def index
@brands = Brand.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @brands }
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的routes.rb
Arbitrary::Application.routes.draw do
resources :brands do
resources :subbrands
end
resources :subbrands do
resources :subsubbrands
end
Run Code Online (Sandbox Code Playgroud)
这是我的brand.rb模型
class Brand < ActiveRecord::Base
validates :name, :presence => …Run Code Online (Sandbox Code Playgroud) 假设我有以下数组:
[['a'],['b'],['c']]
Run Code Online (Sandbox Code Playgroud)
我想在我的表中创建 3 个新行...让我们称表为“信件”
所以我会有以下内容
id name
1 a
2 b
3 c
Run Code Online (Sandbox Code Playgroud)
无论如何,只需一步就可以在 Rails 中做到这一点吗?
我有以下内容:
@products = {
2 => [
#<Review id: 9, answer01: 3, score: 67, style_id: 2, consumer_id: 2,
branch_id: 2, business_id: 2>
],
15 => [
#<Review id: 10, answer01: 3, score: 67, style_id: 2, consumer_id: 2,
branch_id: 2, business_id: 2>,
#<Review id: 11, answer01: 3, score: 67, style_id: 2, consumer_id: 2,
branch_id: 2, business_id: 2>
]
}
Run Code Online (Sandbox Code Playgroud)
我想平均与每个产品的哈希键相关的所有评论的分数.我怎样才能做到这一点?
所以我想做这样的事情:
@ book.chapters.words.uniq
一本书有很多章节.一章有很多单词.
如何使用活动记录完成?
或者,如何使用原始sql完成?
为了使模型属性只在rails_admin面板中读取,您必须执行以下操作:
config.model Style do
field :review_count do
read_only true
end
end
Run Code Online (Sandbox Code Playgroud)
这样做的问题现在是Style没有显示其他字段.我不想手动浏览每个模型添加这个DSL的字段.有没有办法在默认情况下将所有字段包含为可编辑,同时使一个或两个只读?
我想访问创建它的条件语句中新创建的对象:
if saved_object = Branch.new(params[:object]).save
puts saved_object.id
end
Run Code Online (Sandbox Code Playgroud) 在我的控制器中,我曾经能够说:
if params[:business][:branch]
Run Code Online (Sandbox Code Playgroud)
在 Rails 4 之后,当我尝试相同时,我得到:
NoMethodError: undefined method `[]' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
这是我现在能找到的在一行中完成的唯一方法。
params.has_key?(:business) ? params[:business].has_key?(:branch_id) : false
Run Code Online (Sandbox Code Playgroud)
有点啰嗦。
嗨我在我的项目中使用活动的admin gem
我看不到活动管理仪表板上的注销链接.
我只为用户表的所有用户提供了一个表
的routes.rb
Rails.application.routes.draw do
ActiveAdmin.routes(self)
devise_for :users, controllers: { sessions: 'sessions' , :registrations => "registrations" }
namespace :api do
namespace :v1 do
resources :users
end
end
resources :users
devise_scope :user do
match '/users/sign_in', to: 'sessions#create', via: :post
#registration seems to much cooler for authenticable and other stuff
#match 'api/v1/users' , to: 'registrations#create' , via: :post
end
end
Run Code Online (Sandbox Code Playgroud)
在/ admin/dashboard中
ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }
content title: proc{ I18n.t("active_admin.dashboard") } do
div class: …Run Code Online (Sandbox Code Playgroud) 我在尝试:
Product.first.attributes.map{|k, v| "#{k.to_sym} => #{v}"}
Run Code Online (Sandbox Code Playgroud)
但是,我收到如下输出:
["id => 53", "name = blah"], ["id => 54", "name = blahblah"]
Run Code Online (Sandbox Code Playgroud)
我想要的是:
[{:id=>53,:name=>"blah"}, {:id=>54,:name=>"blahblah"}]
Run Code Online (Sandbox Code Playgroud) 我有以下内容:
array_of_hashes = [{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}]
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
new_hash = [{"cow" => 1, "dog" => 2}, {"cow" => 3, "dog" => 4}]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
new_hash = {}
array_of_hashes.each do |a|
new_hash["cow"] = a["a"]
new_hash["dog"] = a["b"]
end
Run Code Online (Sandbox Code Playgroud)
但是,new_hash仅产生第二个哈希array_of_hashes:
new_hash = {"cow"=>3, "dog"=>4}
Run Code Online (Sandbox Code Playgroud)
对于那些需要明确说明问题的人:我如何映射array_of_hashes到get new_hash(如前两行代码中所定义)?