我正在研究两种具有一对一关系的模型;更新方法是删除它想要更新和创建新记录的记录。
这两个模型 - user.rb和profile.rb
USER.RB
class ProfilesController < ApplicationController
def edit
@profile = User.find(params[:user_id]).profile
end
def show
@profile = User.find(params[:user_id]).profile
end
def update
@profile = User.find(params[:user_id]).profile
if @profile.update_attributes(profile_params)
flash[:success] = "profile updated"
redirect_to user_profile_path(current_user, @profile)
else
render 'edit'
end
end
private
def profile_params
params.require(:profile).permit(:id, :name, :street, :city, :state, :zipcode)
end
end
PROFILE.RB
<% provide(:title, "Edit Profile") %>
<h1>Update your profile</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for [:user, @profile] do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
他们的控制器
用户控制器
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :street %>
<%= f.text_field :street, class: 'form-control' %>
<%= f.label :city %>
<%= f.text_field :city, class: 'form-control' %>
<%= f.label :state %>
<%= f.text_field :state, class: 'form-control' %>
<%= f.label :zipcode %>
<%= f.text_field :zipcode, class: 'form-control' %>
PROFILE CONTROLLER
Started GET "/users/1/profile/edit" for 127.0.0.1 at 2015-08-11 20:27:27 -0400
Processing by ProfilesController#edit as HTML
Parameters: {"user_id"=>"1"}
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Profile Load (1.0ms) SELECT `profiles`.* FROM `profiles` WHERE profiles`.`user_id` = 1 LIMIT 1
Rendered profiles/_fields.html.erb (9.0ms)
Rendered profiles/edit.html.erb within layouts/application (30.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered layouts/_header.html.erb (15.0ms)
Rendered layouts/_footer.html.erb (2.0ms)
Completed 200 OK in 1896ms (Views: 1874.9ms | ActiveRecord: 5.0ms)
Started PATCH "/users/104/profile" for 127.0.0.1 at 2015-08-11 20:27:34 -0400
Processing by ProfilesController#update as HTML
Parameters: {"utf8"=>"√","authenticity_token"=>"h7gQEkz8JV/u4zT2qX7ivxRz9FLRUi4K42h55mokP/0=", "profile"=>{"name"=>"Example Test", "street"=>"75 Barracks Rd", "city"=>"Water", "state"=>"AW", "zipcode"=>"23455"}, "commit"=>"Save changes","user_id"=>"104"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 104 LIMIT 1
Completed 404 Not Found in 47ms
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=104):
app/controllers/profiles_controller.rb:19:in `update'
个人资料编辑表单
Started GET "/users/1/edit" for 127.0.0.1 at 2015-08-11 19:16:54 -0400
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered shared/_error_messages.html.erb (57.4ms)
Profile Load (0.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
Rendered users/_fields.html.erb (835.0ms)
Rendered users/edit.html.erb within layouts/application (1193.9ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (90.4ms)
Rendered layouts/_footer.html.erb (265.6ms)
Completed 200 OK in 3979ms (Views: 3962.7ms | ActiveRecord: 3.0ms)
Started PATCH "/users/1" for 127.0.0.1 at 2015-08-11 19:17:11 -0400
Processing by UsersController#update as HTML
Parameters:{"utf8"=>"√","authenticity_token"=>"h7gQEkz8JV/u4zT2qX7ivxRz9FLRUi4K42h55mokP/0=", "user"=>{"profile_attributes"=>{"name"=>"Example Test", "id"=>"103", "street"=>"75 Barracks Rd", "city"=>"Water", "state"=>"AW", "zipcode"=>"23455"}, "email"=>"example@test.org", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save changes", "id"=>"1"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Unpermitted parameters: id (14.0ms) BEGIN
Profile Load (2.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
SQL (212.4ms) DELETE FROM `profiles` WHERE `profiles`.`id` = 103
User Exists (143.1ms) SELECT 1 AS one FROM `users` WHERE (`users`.`email` = 'example@test.org' AND `users`.`id` != 1) LIMIT 1
SQL (53.0ms) UPDATE `users` SET `password_digest` =$2a$10$1KoyfbAwjKYVlwCsbB4WpObW5D.giibIU3pK5hRYW4.iwc11ZEAVS', `updated_at` = '2015-08-11 23:17:14' WHERE `users`.`id` = 1
SQL (31.0ms) INSERT INTO `profiles` (`city`, `created_at`, `name`, state`, `street`, `updated_at`, `user_id`, `zipcode`) VALUES ('Water', '2015-08-11 23:17:15', 'Example Test', 'AW', '75 Barracks Rd', '2015-08-11 23:17:15', 1, '23455') (94.1ms) COMMIT
Redirected to http://localhost:3000/users/1/profile
Completed 302 Found in 4234ms (ActiveRecord: 551.7ms)
Started GET "/users/1/profile" for 127.0.0.1 at 2015-08-11 19:17:15 -0400
Processing by ProfilesController#show as HTML
Parameters: {"user_id"=>"1"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Profile Load (1.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered profiles/show.html.erb within layouts/application (9.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered layouts/_header.html.erb (9.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 1917ms (Views: 1898.3ms | ActiveRecord: 5.0ms)
APP /视图/ PROFILES / _FIELDS.HTML.ERB
Delete
服务器日志
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
resource :profile, only: [:show, :edit, :update ]
end
end
1如何变成104; / users / 1 / profile / edit to / users / 104 / profile?
当我更新用户即/ users / 1 / edit时,我得到了
{{1}}
我很遗憾触发{{1}}命令,感谢任何帮助。
ROUTES FOLDER
Rails.application.routes.draw做
{{1}}
答案 0 :(得分:0)
您错过了id
上允许的属性列表中的UsersController
:
def user_params
params.require(:user).permit(:id, :email, :password, :password_confirmation, profile_attributes: [:id, :name,
:street, :city, :state, :zipcode] )
end