我猜因为参数问题,我无法更新。即使flash表示“更新成功”,也没有任何改变。我使用了User
模型和Skill
模型,这使事情变得更加复杂。
请告诉我如何解决这种情况。
技能控制者:
class SkillsController < UsersController
def update
@skill=Skill.find_by(id:params[:id])
if @skill.update_attributes(skills_params)
flash[:success]="Updated Successfully"
redirect_to users_url
else
flash[:danger]="no infomation"
render @skill
end
end
private
def skills_params
params.permit(:id,:skill_type, :tech, :web_name, :web_url, :web_image, :experience)
end
end
skills / edit.html.erb:
<%= form_for @skill do |f| %>
<%= f.label:skill_type %>
<%= text_field :skill_type, value=@skill.skill_type, :placeholder =>
@skill.skill_type %>
<%= f.label:tech %>
<%= text_field :tech, value=@skill.tech,:placeholder => @skill.tech %>
<!-- More fields -->
<%= f.submit %>
<% end %>
服务器日志:
Processing by SkillsController#update as HTML
Parameters{"utf8"=>"✓","authenticity_token"=>"XrbQqewGHBC8yoFHFg9tkg9sCTtscV+QjUMgaw2pdXEsUk+NiCJHSHVkj/N/bhjD1uaExeop4uSXb6hCCKGD/Q==", "skill_type"=>{"0"=>"1"}, "tech"=>{"shitunnkokoko"=>"aaaa"}, "web_name"=>{"ssasdesilgffgfo"=>"aaaaaa"}, "web_url"=>{"googleeee.com"=>"aaa@aaaa"}, "web_image"=>{"dfsafsafasfasdf"=>"bbbbb"}, "experience"=>{"javaaaaaa"=>"sssss"}, "commit"=>"変更する", "id"=>"5"}
Skill Load (0.1ms) SELECT "skills".* FROM "skills" WHERE "skills"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
Unpermitted parameters: :utf8, :_method, :authenticity_token, :skill_type, :tech, :web_name, :web_url, :web_image, :experience, :commit
(0.1ms) begin transaction
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
(0.0ms) commit transaction
**
**
skill.ruby
class Skill < ApplicationRecord
belongs_to :user
# mount_uploader :picture, PictureUploader
validates :user_id,presence:true
validates :experience, length:{maximum:500}
end
user.rb
class User < ApplicationRecord
before_save {self.email = email.downcase}
validates :name, presence:true,length:{maximum:50}
VALID_EMAIL_FORM=/\A[a-zA-Z0-9_\#!$%&`'*+\-{|}~^\/=?\.]+@[a-zA-Z0-9]
[a-zA-Z0-9\.-]+\z/
validates :email, presence:true,length:{maximum:255},
format: { with: VALID_EMAIL_FORM},
uniqueness:{ case_sensitive: false }
has_secure_password
validates :password, length:{minimum:6},presence:true
def skills
return Skill.find_by(user_id:self.id)
end
end
技能表架构
sqlite> .schema skills
CREATE TABLE "skills" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT
NULL, "skill_type" integer DEFAULT NULL, "tech" varchar DEFAULT NULL,
"web_name" varchar DEFAULT NULL, "web_url" text DEFAULT NULL,
"web_image" varchar DEFAULT NULL, "experience" text DEFAULT NULL,
"created_at" datetime NOT NULL, "updated_at" datetime NOT NULL,
"picture" varchar DEFAULT NULL, "user_id" integer);
CREATE INDEX "index_skills_on_user_id_and_created_at" ON "skills"
("created_at");
CREATE INDEX "index_skills_on_user_id" ON "skills" ("user_id");
用户表架构
sqlite> .schema users
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" varchar DEFAULT NULL, "email" varchar DEFAULT NULL,
"created_at" datetime NOT NULL, "updated_at" datetime NOT NULL,
"password_digest" varchar DEFAULT NULL);
CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
答案 0 :(得分:1)
我的猜测是@skill
为空。
在edit
的{{1}}操作中,您以相同的ID(SkillsController
)加载@skill
和@user
id:params[:id]