0

ユーザープロファイルサブモジュールをユーザーモジュールに追加しようとしていますが、いくつか問題があります。

ルート:

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
4

2 に答える 2