我有与装置和后没有设计出子模型建立父模型嵌套形式。 我工作具有用户模型领域和嵌套在这些专家的模型字段编辑表单上。 更新/保存的作品,但我注销了,给我一个错误说“您需要登录或继续之前注册”当我再次登录我看到领域都得到了正确更新。 我想它不会签我,并告诉我更新的编辑页面时提交表单。
用户模式 - 用色器件内置。
class User < ActiveRecord::Base
// some stuff
has_one :expert
accepts_nested_attributes_for :expert, :update_only => true
// more stuff
专家模式 - 建立无色器件。
class Expert < ActiveRecord::Base
belongs_to :user
用户控制器:
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, :only => [:show, :edit]
def edit
@user = User.friendly.find(params[:id])
@title = "Edit Profile"
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:id, :name, :email, :phone, :password, :role, :expert_attributes => [:id, :Location])
end
end
专家控制器:
def edit
@expert = Expert.find(params[:id])
@title = "Edit Expert Profile"
end
编辑用户视图形式局部:
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Name %><br>
<%= f.text_field :name, :class => "form-control" %>
</div>
<div class="field">
<%= f.label :Email %><br>
<%= f.text_field :email, :class => "form-control" %>
</div>
<div class="field">
<%= f.label :Phone %><br>
<%= f.text_field :phone, :class => "form-control" %>
</div>
<div class="field">
<%= f.label :Password %><br>
<%= f.text_field :password, :class => "form-control" %>
</div>
<div class="field">
<%= f.fields_for :expert do |e| %>
<%= e.label :Location %><br />
<%= e.text_field :Location %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>