我正在编写一个具有以下模型设计的应用程序。
Model A has_many bs
Model B belongs_to a
b is sigular an bs/BS is plural
a is singular and as/AS is plural
我正在使用管理员控制器来定义用户可以执行的操作
class Admin::BsController < ApplicationController
def create
@a = A.find(params[:a_id])
@b = @a.bs.create(entry_params)
redirect_to a_path(@a)
end
def new
@b = B.new
end
end
视图中的表单是show.html.erb,生成类似
<%= form_with(model: [:admin, @a,B.new ], local: true) do |f| %>
<%= end %>
告诉我们应该发生什么 应该创建属于a的新DB条目。
我收到错误
NoMethodError in Admin::EntriesController#create
undefined method `string' for #<B:0x00007f93a8090450> Did you mean? String
Rails版本:Rails 5.1.5
Ruby版:Ruby 2.5.0p0
更新 这个想法是用户可以创建一个带有个性化ID(admin_key)的列表,如涂鸦
模型A是列表
模型B是A
的条目并且可以与public_key共享该条目) 那就跑了! 问题是在列表中添加新条目 管理员控制只有owner-url可以运行CRUD功能到列表 架构文件
ActiveRecord::Schema.define(version: 20161023094929) do
create_table "admin_lists", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "entries", force: :cascade do |t|
t.string "title"
t.string "email"
t.string "description"
t.boolean "state"
t.integer "list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["list_id"], name: "index_entries_on_list_id"
end
create_table "lists", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "email"
t.string "admin_key"
t.string "public_key"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "exp_date"
t.string "state"
end
end
完整的stackstrace
activemodel (5.1.5) lib/active_model/attribute_methods.rb:432:in `method_missing'
activemodel (5.1.5) lib/active_model/validator.rb:148:in `block in validate'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `each'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `validate'
activesupport (5.1.5) lib/active_support/callbacks.rb:413:in `block in make_lambda'
activesupport (5.1.5) lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `catch'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `block in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:198:in `block in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:130:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.5) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations.rb:335:in `valid?'
activerecord (5.1.5) lib/active_record/validations.rb:65:in `valid?'
activerecord (5.1.5) lib/active_record/validations.rb:82:in `perform_validations'
activerecord (5.1.5) lib/active_record/validations.rb:44:in `save'
activerecord (5.1.5) lib/active_record/attribute_methods/dirty.rb:35:in `save'
activerecord (5.1.5) lib/active_record/transactions.rb:308:in `block (2 levels) in save'
activerecord (5.1.5) lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
activerecord (5.1.5) lib/active_record/transactions.rb:308:in `block in save'
activerecord (5.1.5) lib/active_record/transactions.rb:323:in `rollback_active_record_state!'
activerecord (5.1.5) lib/active_record/transactions.rb:307:in `save'
activerecord (5.1.5) lib/active_record/suppressor.rb:42:in `save'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:371:in `insert_record'
activerecord (5.1.5) lib/active_record/associations/has_many_association.rb:34:in `insert_record'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:360:in `block (2 levels) in _create_record'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:447:in `replace_on_target'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:281:in `add_to_target'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:358:in `block in _create_record'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:129:in `block in transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/home/marcus/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:128:in `transaction'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:357:in `_create_record'
activerecord (5.1.5) lib/active_record/associations/has_many_association.rb:121:in `_create_record'
activerecord (5.1.5) lib/active_record/associations/association.rb:196:in `create'
activerecord (5.1.5) lib/active_record/associations/collection_proxy.rb:347:in `create'
app/controllers/admin/entries_controller.rb:7:in `create'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
模型/ wishlist.rb
#models/list.rb
class List < ApplicationRecord
has_many :entries
validates :email, presence: true
validates :admin_key, :uniqueness => true
before_create :create_admin_key
before_create :create_public_key
state_machine initial: :inactive do
event :activate do
transition :inactive => :active
end
event :deactivate do
transition :activate => :inactive
end
end
def create_admin_key
begin
self.admin_key = SecureRandom.urlsafe_base64(len=15)
end while self.class.exists?(admin_key: admin_key)
end
def create_public_key
begin
self.public_key = SecureRandom.urlsafe_base64(len=8)
end while self.class.exists?(public_key: public_key)
end
# defined the public_key as the link to navigate the wishlist
def to_param
admin_key
end
end
模型/ entry.rb
#models/entry.rb
class Entry < ApplicationRecord
belongs_to :wishlist, optional: true
*validates :string, :uniqueness => true
before_create :create_entry_key
def create_entry_key
begin
self.string = SecureRandom.urlsafe_base64(len=5)
* end while self.class.exists?(string: string)
end
end
我希望这对你有所帮助。我没有发现错误。
答案 0 :(得分:0)
Bug位于Model entry.rb中。 valiation是模型是模板。这是正确的代码。
破损和固定的代码标有箭头。这只是问题,只有字符串,没有变量。在我的情况下,entry_key。
#models/entry.rb
class Entry < ApplicationRecord
belongs_to :wishlist, optional: true
before_create :create_entry_key
validates_presence_of :wishlist
validates :entry_key, :uniqueness => true # <-
def create_entry_key
begin
self.entry_key = SecureRandom.urlsafe_base64(len=5) # <-
end while self.class.exists?(entry_key: entry_key) # <-
end
end