Rails form_for 用 Nil 填充模型
Rails form_for populates model with Nil
我无法使用从 form_for 传递的参数填充我的模型,因为它将 Nil 分配给 id、我的列(已解密)和 created/updated 字段。
以下是我的看法(new.html.erb):
<%= form_for @decrypt_text, url: { action: "create" } do |f| %>
<div class="field">
<%= f.label :decrypt_text %><br>
<%= f.text_area :decrypted %><br>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
和我的控制器:
class DecryptController < ApplicationController
def new
@decrypt_text = Dmessage.new
end
def create
@decrypt_text = Dmessage.new(params[:decrypted])
p @decrypt_text
if @decrypt_text.save
redirect_to '/decrypt/display'
else
redirect_to '/'
end
end
def display
@displayme = Dmessage.order("created_at").last
end
end
为了以防万一,我的模型:
class CreateDmessages < ActiveRecord::Migration
def change
create_table :dmessages do |t|
t.text :decrypted
t.timestamps null: false
end
end
end
我知道它分配的是 nil 值,因为:
p @decrypt_text
打印出来:
#<Dmessage id: nil, decrypted: nil, created_at: nil, updated_at: nil>
我真的不确定我错过了什么,但我是 Rails 的新手。感谢您的帮助!
首先,从您的表单中删除 url: { action: "create" }
。这是不必要的,因为 Rails 足够聪明,可以理解如果 @decrypt_text
不存在,它将触发必要的 create
动作。
但是,我认为真正给您带来麻烦的是您没有在应用程序中使用 强参数 (@decrypt_text = Dmessage.new(params[:decrypted])
)。您正在尝试直接初始化 params 哈希,这非常危险。从 Rails 版本 4.x 开始,不允许以这种方式直接初始化参数散列,必须使用强参数,以防止批量分配。
这些将是我个人在您的控制器文件中所做的更改...
class DecryptController < ApplicationController
def new
@decrypt_text = Dmessage.new
end
def create
@decrypt_text = Dmessage.new(decrypt_params)
if @decrypt_text.save
redirect_to '/decrypt/display'
else
redirect_to '/'
end
end
def display
@displayme = Dmessage.order("created_at").last
end
private
def decrypt_params
params.require(:dmessage).permit(:decrypted)
end
end
如果你使用 rails 4.x 那么你必须使用 Rails 版本的强参数 4.x 不允许直接初始化参数哈希.
将以下私有方法添加到您的控制器中,
定义 decrypt_params
params.require(:dmessage).permit(:解密)
结束
因为你使用的是Rails 3,问题是这一行
@decrypt_text = Dmessage.new(params[:decrypted])
应该是
@decrypt_text = Dmessage.new(params[:dmessage])
我无法使用从 form_for 传递的参数填充我的模型,因为它将 Nil 分配给 id、我的列(已解密)和 created/updated 字段。
以下是我的看法(new.html.erb):
<%= form_for @decrypt_text, url: { action: "create" } do |f| %>
<div class="field">
<%= f.label :decrypt_text %><br>
<%= f.text_area :decrypted %><br>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
和我的控制器:
class DecryptController < ApplicationController
def new
@decrypt_text = Dmessage.new
end
def create
@decrypt_text = Dmessage.new(params[:decrypted])
p @decrypt_text
if @decrypt_text.save
redirect_to '/decrypt/display'
else
redirect_to '/'
end
end
def display
@displayme = Dmessage.order("created_at").last
end
end
为了以防万一,我的模型:
class CreateDmessages < ActiveRecord::Migration
def change
create_table :dmessages do |t|
t.text :decrypted
t.timestamps null: false
end
end
end
我知道它分配的是 nil 值,因为:
p @decrypt_text
打印出来:
#<Dmessage id: nil, decrypted: nil, created_at: nil, updated_at: nil>
我真的不确定我错过了什么,但我是 Rails 的新手。感谢您的帮助!
首先,从您的表单中删除 url: { action: "create" }
。这是不必要的,因为 Rails 足够聪明,可以理解如果 @decrypt_text
不存在,它将触发必要的 create
动作。
但是,我认为真正给您带来麻烦的是您没有在应用程序中使用 强参数 (@decrypt_text = Dmessage.new(params[:decrypted])
)。您正在尝试直接初始化 params 哈希,这非常危险。从 Rails 版本 4.x 开始,不允许以这种方式直接初始化参数散列,必须使用强参数,以防止批量分配。
这些将是我个人在您的控制器文件中所做的更改...
class DecryptController < ApplicationController
def new
@decrypt_text = Dmessage.new
end
def create
@decrypt_text = Dmessage.new(decrypt_params)
if @decrypt_text.save
redirect_to '/decrypt/display'
else
redirect_to '/'
end
end
def display
@displayme = Dmessage.order("created_at").last
end
private
def decrypt_params
params.require(:dmessage).permit(:decrypted)
end
end
如果你使用 rails 4.x 那么你必须使用 Rails 版本的强参数 4.x 不允许直接初始化参数哈希.
将以下私有方法添加到您的控制器中, 定义 decrypt_params params.require(:dmessage).permit(:解密) 结束
因为你使用的是Rails 3,问题是这一行
@decrypt_text = Dmessage.new(params[:decrypted])
应该是
@decrypt_text = Dmessage.new(params[:dmessage])