Ruby on rails Rails在模型中分配id
我想为注册分配事件选项id。通过将以下内容添加到表单中,我可以在视图中轻松完成此操作:Ruby on rails Rails在模型中分配id,ruby-on-rails,Ruby On Rails,我想为注册分配事件选项id。通过将以下内容添加到表单中,我可以在视图中轻松完成此操作: <%= f.text_field :event_option_id, value: @event_option.id %> #app/controllers/registrations_controller.rb class RegistrationsController < ApplicationController def create @order
<%= f.text_field :event_option_id, value: @event_option.id %>
#app/controllers/registrations_controller.rb
class RegistrationsController < ApplicationController
def create
@order = current_order
@registration = @order.registrations.new registration_params
@registration.save
end
private
def registration_params
params.require(:registration).permit(:event_option_id, :other, :params)
end
end
日志中出现错误:
class Registration < ActiveRecord::Base
belongs_to :event_option
belongs_to :order_item
belongs_to :order
before_save :set_event_options
def order_present
if order.nil?
errors.add(:order, "is not a valid order.")
end
end
def registration_price
self[:price] = event_option.price
end
def event_option_id
self.event_option_id = event_option
end
private
def set_event_options
self[:price] = registration_price
self.event_option_id = event_option_id
end
end
class EventOption < ActiveRecord::Base
belongs_to :event
has_many :registrations
end
def create
@event_option = EventOption.find(params[:id])
@order = current_order
@registration = @order.registrations.build(registration_params)
#@registration = Registration.new(registration_params)
@order_id = current_order.id
respond_to do |format|
if @registration.save
format.html { redirect_to @registration, notice: 'Registration was successfully created.' }
format.json { render :show, status: :created, location: @registration }
format.js {}
@order.save
session[:order_id] = @order.id
else
format.html { render :new }
format.json { render json: @registration.errors, status: :unprocessable_entity }
end
end
Started POST "/registrations" for 127.0.0.1 at 2016-01-04 21:16:06 -0500
Processing by RegistrationsController#create as JS
Parameters: {"utf8"=>"âo"", "registration"=>{"name"=>"saasas", "lastname"=>"asas"}, "commit"=>"Create Registration"}
EventOption Load (0.0ms) SELECT "event_options".* FROM "event_options" WHERE "event_options"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound (Couldn't find EventOption with 'id'=):
app/controllers/registrations_controller.rb:27:in `create'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (44.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (48.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (105.0ms)
Rails.application.routes.draw do
resource :cart, only: [:show]
resources :orders
resources :order_items
resources :registrations
resources :event_options
resources :events
resources :charges
root 'events#index'
<%= form_for(@registration, remote: true) do |f| %>
<% if @registration.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@registration.errors.count, "error") %> prohibited this registration from being saved:</h2>
<ul>
<% @registration.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我正在阅读rails文档的这一部分:
,
但还是不知道发生了什么
更新:
class Registration < ActiveRecord::Base
belongs_to :event_option
belongs_to :order_item
belongs_to :order
before_save :set_event_options
def order_present
if order.nil?
errors.add(:order, "is not a valid order.")
end
end
def registration_price
self[:price] = event_option.price
end
def event_option_id
self.event_option_id = event_option
end
private
def set_event_options
self[:price] = registration_price
self.event_option_id = event_option_id
end
end
class EventOption < ActiveRecord::Base
belongs_to :event
has_many :registrations
end
def create
@event_option = EventOption.find(params[:id])
@order = current_order
@registration = @order.registrations.build(registration_params)
#@registration = Registration.new(registration_params)
@order_id = current_order.id
respond_to do |format|
if @registration.save
format.html { redirect_to @registration, notice: 'Registration was successfully created.' }
format.json { render :show, status: :created, location: @registration }
format.js {}
@order.save
session[:order_id] = @order.id
else
format.html { render :new }
format.json { render json: @registration.errors, status: :unprocessable_entity }
end
end
Started POST "/registrations" for 127.0.0.1 at 2016-01-04 21:16:06 -0500
Processing by RegistrationsController#create as JS
Parameters: {"utf8"=>"âo"", "registration"=>{"name"=>"saasas", "lastname"=>"asas"}, "commit"=>"Create Registration"}
EventOption Load (0.0ms) SELECT "event_options".* FROM "event_options" WHERE "event_options"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound (Couldn't find EventOption with 'id'=):
app/controllers/registrations_controller.rb:27:in `create'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (44.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (48.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (105.0ms)
Rails.application.routes.draw do
resource :cart, only: [:show]
resources :orders
resources :order_items
resources :registrations
resources :event_options
resources :events
resources :charges
root 'events#index'
<%= form_for(@registration, remote: true) do |f| %>
<% if @registration.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@registration.errors.count, "error") %> prohibited this registration from being saved:</h2>
<ul>
<% @registration.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
路线:
class Registration < ActiveRecord::Base
belongs_to :event_option
belongs_to :order_item
belongs_to :order
before_save :set_event_options
def order_present
if order.nil?
errors.add(:order, "is not a valid order.")
end
end
def registration_price
self[:price] = event_option.price
end
def event_option_id
self.event_option_id = event_option
end
private
def set_event_options
self[:price] = registration_price
self.event_option_id = event_option_id
end
end
class EventOption < ActiveRecord::Base
belongs_to :event
has_many :registrations
end
def create
@event_option = EventOption.find(params[:id])
@order = current_order
@registration = @order.registrations.build(registration_params)
#@registration = Registration.new(registration_params)
@order_id = current_order.id
respond_to do |format|
if @registration.save
format.html { redirect_to @registration, notice: 'Registration was successfully created.' }
format.json { render :show, status: :created, location: @registration }
format.js {}
@order.save
session[:order_id] = @order.id
else
format.html { render :new }
format.json { render json: @registration.errors, status: :unprocessable_entity }
end
end
Started POST "/registrations" for 127.0.0.1 at 2016-01-04 21:16:06 -0500
Processing by RegistrationsController#create as JS
Parameters: {"utf8"=>"âo"", "registration"=>{"name"=>"saasas", "lastname"=>"asas"}, "commit"=>"Create Registration"}
EventOption Load (0.0ms) SELECT "event_options".* FROM "event_options" WHERE "event_options"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound (Couldn't find EventOption with 'id'=):
app/controllers/registrations_controller.rb:27:in `create'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (44.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (48.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (105.0ms)
Rails.application.routes.draw do
resource :cart, only: [:show]
resources :orders
resources :order_items
resources :registrations
resources :event_options
resources :events
resources :charges
root 'events#index'
<%= form_for(@registration, remote: true) do |f| %>
<% if @registration.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@registration.errors.count, "error") %> prohibited this registration from being saved:</h2>
<ul>
<% @registration.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
登记表-内部活动选项show.html.erb:
class Registration < ActiveRecord::Base
belongs_to :event_option
belongs_to :order_item
belongs_to :order
before_save :set_event_options
def order_present
if order.nil?
errors.add(:order, "is not a valid order.")
end
end
def registration_price
self[:price] = event_option.price
end
def event_option_id
self.event_option_id = event_option
end
private
def set_event_options
self[:price] = registration_price
self.event_option_id = event_option_id
end
end
class EventOption < ActiveRecord::Base
belongs_to :event
has_many :registrations
end
def create
@event_option = EventOption.find(params[:id])
@order = current_order
@registration = @order.registrations.build(registration_params)
#@registration = Registration.new(registration_params)
@order_id = current_order.id
respond_to do |format|
if @registration.save
format.html { redirect_to @registration, notice: 'Registration was successfully created.' }
format.json { render :show, status: :created, location: @registration }
format.js {}
@order.save
session[:order_id] = @order.id
else
format.html { render :new }
format.json { render json: @registration.errors, status: :unprocessable_entity }
end
end
Started POST "/registrations" for 127.0.0.1 at 2016-01-04 21:16:06 -0500
Processing by RegistrationsController#create as JS
Parameters: {"utf8"=>"âo"", "registration"=>{"name"=>"saasas", "lastname"=>"asas"}, "commit"=>"Create Registration"}
EventOption Load (0.0ms) SELECT "event_options".* FROM "event_options" WHERE "event_options"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound (Couldn't find EventOption with 'id'=):
app/controllers/registrations_controller.rb:27:in `create'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (44.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (48.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (105.0ms)
Rails.application.routes.draw do
resource :cart, only: [:show]
resources :orders
resources :order_items
resources :registrations
resources :event_options
resources :events
resources :charges
root 'events#index'
<%= form_for(@registration, remote: true) do |f| %>
<% if @registration.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@registration.errors.count, "error") %> prohibited this registration from being saved:</h2>
<ul>
<% @registration.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
禁止保存此注册:
我认为您的控制器的创建
功能缺少参数[:id]
最简单的修复方法是将其添加到表单中:
<%= form_for(@registration, remote: true) do |f| %>
<%= hidden_field_tag :id, your_event_option_id %>
### your other stuffs
###你的其他东西
我认为您的控制器的创建
功能缺少参数[:id]
最简单的修复方法是将其添加到表单中:
<%= form_for(@registration, remote: true) do |f| %>
<%= hidden_field_tag :id, your_event_option_id %>
### your other stuffs
###你的其他东西
该错误明确指出,Rails在没有id的情况下无法找到事件选项
:
def create
@event_option = EventOption.find params[:id] #-> :id is not passed to create action
要修复它,只需使用作为表单一部分提交的参数
:
<%= f.text_field :event_option_id, value: @event_option.id %>
#app/controllers/registrations_controller.rb
class RegistrationsController < ApplicationController
def create
@order = current_order
@registration = @order.registrations.new registration_params
@registration.save
end
private
def registration_params
params.require(:registration).permit(:event_option_id, :other, :params)
end
end
这将设置事件选项\u id
作为顶级参数散列的一部分,该散列将作为参数[:事件选项\u id]
传递给控制器(正如您已经拥有的):
该错误明确指出,Rails在没有id
的情况下无法找到EventOption
:
def create
@event_option = EventOption.find params[:id] #-> :id is not passed to create action
要修复它,只需使用作为表单一部分提交的参数
:
<%= f.text_field :event_option_id, value: @event_option.id %>
#app/controllers/registrations_controller.rb
class RegistrationsController < ApplicationController
def create
@order = current_order
@registration = @order.registrations.new registration_params
@registration.save
end
private
def registration_params
params.require(:registration).permit(:event_option_id, :other, :params)
end
end
这将设置事件选项\u id
作为顶级参数散列的一部分,该散列将作为参数[:事件选项\u id]
传递给控制器(正如您已经拥有的):
你能发布更多的日志吗?你要走哪条路线?注册是事件选项的子项吗?Post routes,更多日志(包括参数),在视图中查看表单将非常棒。我已使用日志和路由更新Post。注册是事件选项的子项,但我不嵌套路由。嵌套路由,这样您就可以获得事件选项\u id
,如果您不想通过视图发送。您知道为什么我可以通过event_选项的价格设置注册的价格,但不能对id执行相同的操作吗?您是否在参数中发送价格?它不在日志中扫描你发布更多日志了吗?你要走哪条路线?注册是事件选项的子项吗?Post routes,更多日志(包括参数),在视图中查看表单将非常棒。我已使用日志和路由更新Post。注册是事件选项的子项,但我不嵌套路由。嵌套路由,这样您就可以获得事件选项\u id
,如果您不想通过视图发送。您知道为什么我可以通过event_选项的价格设置注册的价格,但不能对id执行相同的操作吗?您是否在参数中发送价格?感谢你的帮助。谢谢你的帮助。这和我以前的工作差不多。关于这一点,用户可以使用dev工具来更改事件选项的id。我要做的是在模型中设置事件选项id。这样我就不必在表单中包含文本或隐藏字段。我现在直接从模型中传递事件选项的注册价格。这很好用。这就是为什么我不明白为什么它不能与id一起工作。谢谢你的帮助。这和我以前工作得很好的情况差不多。关于这一点,用户可以使用dev工具来更改事件选项的id。我要做的是在模型中设置事件选项id。这样我就不必在表单中包含文本或隐藏字段。我现在直接从模型中传递事件选项的注册价格。这很好用。这就是为什么我不明白为什么它不能与id一起工作。谢谢你的帮助。