我的项目中有一个使用Ruby on Rails(Ruby 2.4.0和Rails 5.2.0)的联系表单。当我尝试发送表单时,会发生以下错误:
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError:
app/controllers/contacts_controller.rb:7:in `create'
我怀疑错误在接触控制器中,正如错误所示。
contacts_controller.rb
class ContactsController < InheritedResources::Base
def index
end
def create
@contact = Contact.new(params[:contact])
if @contact.save
ContactMailer.delay_for(10.seconds, retry: true).create(@contact)
render nothing: true, status: 200
else
render nothing: true, status: 400
end
end
end
在一些旧版本的Ruby和Rails中,相同的代码工作正常。最新版本有什么变化吗?有人可以帮帮我吗?
感谢。
答案 0 :(得分:4)
我认为你应该以某种方式改变它:
class ContactsController < InheritedResources::Base
def index
end
def create
@contact = Contact.new(contact_params)
if @contact.save
ContactMailer.delay_for(10.seconds, retry: true).create(@contact)
render nothing: true, status: 200
else
render nothing: true, status: 400
end
end
private
def contact_params
params.require(:contact).permit(:name, :phone)
end
end