2013-10-14 50 views
0

我試圖編輯文章時收到錯誤:軌道4,參數錯誤未知密鑰,更新控制器

帖子#編輯器:

def edit 
@post = Post.find params[:id] 
end 

帖子#更新控制器:

def update 
@post = Post.find params[:post] 

if @post.update_attributes params[:post] 
    redirect_to posts_path 
else 
    render 'edit' 
end 
end 

編輯觀點:

h1 Edit Post 
= form_for @post do |f| 
p 
    = f.label :title 
    = f.text_field :title 
p 
    br 
p 
    = f.label :content 
    = f.text_area :content 
p 
    br 
p 
    = f.submit 'Update Post' 
p 
    br 

這是我在PostsController#update,Unknown Key:title錯誤中得到ArgumentError錯誤。我仍然環繞我的頭周圍的Rails 4強參數的概念,所以可能有一些做這個...任何想法?

+0

^h您是否在軌道控制檯中測試了控制器操作?是否'Post.find(ID:)'返回你的期望? – Nikola

+0

什麼是瀏覽器的URL,當你收到此錯誤? – tihom

+0

是尼古拉,Post.find(13)控制檯內返回的期望是什麼... –

回答

2

嘗試下面我希望它會幫助你。

def update 
    @post = Post.find(params[:id]) 
    if @post.update(post_params) 
    redirect_to @post 
    else 
    render 'edit' 
    end 
end 
0

我的職位#更新控制器更新到

params[:id] 

而不是

params[:post] 

此修復程序給我帶來ForbiddenAttributesError造成

if @post.update_attributes params[:post] 

這是由於與導軌4引入的強參數概念...我固定此用強參數

post_params 

從私有方法代替

params[:post] 

post_params

private 
    def post_params 
    params.require(:post).permit(:title, :content) 
end 

最終解決的關鍵未知錯誤:標題