我正在尝试将用户配置文件子模块添加到用户模块但遇到一些问题。
路线:
resources :users do
resources :userprofiles
end
userprofiles_controller.rb:
class UserprofilesController < ApplicationController
def edit
@user = current_user
@user.UserProfile ||= UserProfile.new
@userprofile = @user.UserProfile
end
def update
@user = current_user
@user.UserProfile ||= UserProfile.new
@userprofile = @user.UserProfile
if @userprofile.update_attributes(:userprofile => params[:userprofile])
redirect_to @user
flash[:notice] = "Changes saved."
else
render 'edit'
flash[:notice] = "Error."
end
end
end
user_profile.rb:
class UserProfile < ActiveRecord::Base
attr_accessible :first_name, :last_name, :summary
belongs_to :user
end
错误:
Can't mass-assign protected attributes for UserProfile: userprofile
线:
if @userprofile.update_attributes(:userprofile => params[:userprofile])
编辑
形式:
<%= form_for([@user, @userprofile], url: user_userprofile_path(@user, @userprofile)) do |form| %>
<%= form.label :first_name %>
<%= form.text_field :first_name %>
<%= form.label :last_name %>
<%= form.text_field :last_name %>
<%= form.label :summary %>
<%= form.text_area :summary %>
<%= form.submit "Update", class: "btn btn-block btn-primary" %>
<% end %>
桌子:
create_table "user_profiles", force: true do |t|
t.string "last_name"
t.string "first_name"
t.text "summary"
t.integer "user_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
end