我正在使用ActiveAttr,它通过块选项为您提供了很好的初始化:
person = Person.new() do |p|
p.first_name = 'test'
p.last_name = 'man'
end
Run Code Online (Sandbox Code Playgroud)
但是,在包含ActiveAttr :: Model的特定类中,我想绕过此功能,因为我想将块用于其他内容.所以我们走了:
class Imperator::Command
include ActiveAttr::Model
end
class MyCommand < Imperator::Command
def initialize(*args, &block)
@my_block = block
super(*args)
end
end
Run Code Online (Sandbox Code Playgroud)
这很难失败,因为块仍然传递到链上,并最终在ActiveAttr内,这段代码运行:
def initialize(*)
super
yield self if block_given?
end
Run Code Online (Sandbox Code Playgroud)
所以,如果我的电话如下:
MyCommand.new() { |date| date.advance(month: 1) }
Run Code Online (Sandbox Code Playgroud)
它失败如下:
NoMethodError: undefined method `advance' for #<MyCommand:0x007fe432c4fb80>
Run Code Online (Sandbox Code Playgroud)
因为MyCommand没有方法:提前调用MyCommand显然失败了.
所以我的问题是,有没有一种方法可以在我super再次调用之前从方法签名中删除块,这样块的行程不会超过我重写的初始化程序?
我无法弄清楚这个问题,即使它看起来并不复杂......
我想制作一个表单来在我的rails应用程序中发送电子邮件,但这个不起作用.我收到以下错误:
TypeError in MessagesController#create:
#<Message content: "test", email: "test@test.fr", name: "test"> is not a symbol
发生错误: app/controllers/messages_controller.rb:10:in 'create'
我是messages_controller:
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
Messenger.send(@message).deliver
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
end
Run Code Online (Sandbox Code Playgroud)
形式new.html.erb:
<%= form_for @message do |f| %>
<%= f.text_field :name %>
<%= f.text_field :email %>
<%= f.text_area :content, :rows => 5 …Run Code Online (Sandbox Code Playgroud)