在我的应用程序中,我试图让管理员用户可以编辑“帖子”的一个属性
后模型为:
class PostsController < ApplicationController
before_filter :admin_user, only: [:edit, :update]
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post][:some_attr])
flash[:success] = "Post updated"
redirect_to posts_path
else
redirect_to root_path
end
end
编辑视图是:
<% provide(:title, "Edit post") %>
<h1>Update the post</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@post) do |f| %>
<%= f.label :some_attr %>
<%= f.text_field :some_attr %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
当我尝试在编辑页面的 some_attr test_field 中输入“123”时,它会呈现错误:
NoMethodError in PostsController#update
undefined method `stringify_keys' for "123":String
Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:22:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"EdTg+cFBnZY447oSDqSTPfb/PJ6VisJOrQ8kvichDrE=",
"post"=>{"some_attr"=>"123"},
"commit"=>"Save changes",
"id"=>"17"}
可能是什么问题呢?我错过了哪一块拼图?
谢谢