Ruby on rails 更新操作在rails 4中不起作用
当我更新我的表单时,它会为唯一的字段抛出错误 在我的模型中Ruby on rails 更新操作在rails 4中不起作用,ruby-on-rails,ruby,validation,ruby-on-rails-4,Ruby On Rails,Ruby,Validation,Ruby On Rails 4,当我更新我的表单时,它会为唯一的字段抛出错误 在我的模型中 validates :url, presence: true, uniqueness: true, :if => lambda {self.role_id == 2} 以我的形式 = f.text_field :url, :class => 'form-control' 在我的控制器中 def update respond_to do |format| if @user.update(user_params)
validates :url, presence: true, uniqueness: true, :if => lambda {self.role_id == 2}
以我的形式
= f.text_field :url, :class => 'form-control'
在我的控制器中
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to stores_path, notice: 'Store was successfully updated.' }
else
format.html { render :edit }
end
end
end
即使我没有做任何事情,这是给我的错误,这个网址已经采取。请帮忙
提前谢谢我决定这样做
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to stores_path, notice: 'Store was successfully updated.' }
else
incoming_url = User.where("id=? and url=?",@user.id,params[:user][:url])
if !incoming_url.blank?
format.html { redirect_to stores_path, notice: 'Store was successfully updated.' }
end
format.html { render :edit }
end
end
end
您已经有了一个与您现在尝试输入的URL相同的对象。正如在模型验证中,您说过url应该是唯一的,模型抛出了一个错误。@AlokSwain您是对的。但我只是更新了对象,甚至没有更改任何字段中的字母表。它是如何为自己的字段值抛出错误的。您是否将@user定义为find@techdreams-你能插入更新期间发送的用户对象和参数吗?RubyRacer:Yes def set_user@user=user.findparams[:id]在_操作之前结束:set_user,仅:[:show,:edit,:update,:destroy]Alok:如果我捕获参数值并插入更新,我如何在需要的地方应用验证。这可能是安全漏洞