请帮我解决此错误。执行更新操作时会显示此错误。
错误:
NoMethodError in UsersController#update
undefined method `permit' for :user:Symbol
我的代码片段如下所示。
视图/用户/ edit.html.erb
<h1>Edit your data</h1>
<%= form_for @user,:url => {:action => 'update',:id => params[:id]} do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field:name,:value => @edit.name %>
<%= f.email_field:email,:value => @edit.email %>
<%= f.text_field:message,:value => @edit.message %>
<%= f.submit "Update" %>
<% end %>
<%= link_to "Back",users_index_path %>
控制器/ users_controller.rb
class UsersController < ApplicationController
def index
end
def new
@user=User.new
end
def create
@user=User.new(users_params)
if @user.save
flash[:notice]="Your data is saved succesfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You are entering wrong data"
flash[:color]="invalid"
render :new
end
end
def show
@user=User.all
end
def delete
@user=User.find(params[:id])
if @user.delete
flash[:notice]=@user.name+"has deleted successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]=@user.name+"could not delete.Please check it.."
flash[:color]="invalid"
render :show
end
end
def edit
@edit=User.find(params[:id])
@user=User.new
end
def update
@user=User.find(params[:id])
if @user.update_attributes(update_params)
flash[:notice]="Your data has updated successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Your data could not update..check it.."
flash[:color]="invalid"
render :edit
end
end
private
def users_params
params.require(:user).permit(:name, :email, :message,pets_attributes: [:name, :email,:message])
end
def update_params
params.require (:user).permit(:name,:email,:message,pets_attributes: [:name, :email,:message])
end
end
模型/ user.rb
class User < ActiveRecord::Base
has_many :pets
accepts_nested_attributes_for :pets
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true,:length => { :minimum => 5 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :message, :presence => true
end
请帮我解决上述错误。
答案 0 :(得分:2)
require
和(:user)
之间有空格。您当前的代码相当于:
def update_params
params.require(:user.permit(:name,:email,:message,pets_attributes: [:name, :email,:message]))
end
正如您现在所看到的,您在permit
符号上调用:user
方法,这是导致错误的直接原因。
应该是:
def update_params
params.require(:user).permit(:name,:email,:message,pets_attributes: [:name, :email,:message])
end
BTW有两种完全相同的方法是毫无意义的。
答案 1 :(得分:0)
检查你的问题:
you have a space after require and before (:user)
尝试:
def update_params
params.require(:user).permit(:name,:email,:message,
pets_attributes: [:name, :email,:message])
end
为了更好的方法,你可以只使用一种方法:
def user_params
params.require(:user).permit(:name,:email,:message,
pets_attributes: [:name, :email,:message])
end
您可以只使用一种方法并在新建和编辑中重复使用,而不是为新建和编辑使用单独的过滤方法。
保持简单干燥。