Rails 4嵌套形式与设计记录我的更新 [英] Rails 4 Nested form with devise logs me out on update

查看:173
本文介绍了Rails 4嵌套形式与设计记录我的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的形式,其父模型是用devise构建的,而孩子模型没有设计。我正在编写具有用户模型字段并嵌套在专家模型字段中的编辑表单。更新/保存工作,但记录我,并给我一个错误说你需要登录或注册后继续
当我重新登录,我看到这些字段已正确更新。我希望它不会签署我,并在提交表单时向我显示更新的编辑页面。



用户模型 - 用devise构建。

  class User< ActiveRecord :: Base 
//某些东西
has_one:expert
acceptable_nested_attributes_for:expert,:update_only =>真实的
//更多的东西

专家模型 - 建立与无设计。

  class Expert< ActiveRecord :: Base 
belongs_to:user

用户控制器:

  before_action:set_user只有:[:show,:edit,:update,,destroy] 
before_filter:authenticate_user !,:only =>

def edit
@user = User.friendly.find(params [:id])
@title =编辑个人资料
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
#使用回调来共享动作之间的通用设置或约束。
def set_user
@user = User.friendly.find(params [:id])
end

#从不信任可怕的互联网参数,只允许白名单通过。
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 =编辑专家个人资料
end

编辑用户视图形式partial:

 <%= form_for(@user)do | f | %GT; 
<%if @ user.errors.any? %GT;
< div id =error_explanation>
< h2><%= pluralize(@ user.errors.count,error)%>禁止此用户被保存:< / h2>

< ul>
<%@ user.errors.full_messages.each do | msg | %GT;
< li><%= msg%>< / li>
<%end%>
< / ul>
< / div>
<%end%>

< div class =field>
<%= f.label:名称%>< br>
<%= f.text_field:name,:class => form-control%>
< / div>


< div class =field>
<%= f.label:电子邮件%>< 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:密码%>< br>
<%= f.text_field:password,:class => form-control%>
< / div>

< div class =field>
<%= f.fields_for:expert do | e | %GT;
<%= e.label:位置%>< br />
<%= e.text_field:位置%>
<%end%>
< / div>

< div class =actions>
<%= f.submit%>
< / div>
<%end%>


解决方案

在您的 UsersController ,您应该将:update 添加到 authenticate_user!过滤器:

  before_filter:authenticate_user !,:only => [:show,:edit,,update] 


I have a nested form with the parent model built with devise and the child model without devise. I'm working on the edit form which has the user model fields and nested inside them expert model fields. The update/save works but logs me out and gives me an error saying "You need to sign in or sign up before continuing" When I sign back in I see that the fields have been updated correctly. I would like it to not sign me out and show me the updated Edit page upon submitting the form.

User model - built with devise.

class User < ActiveRecord::Base
// some stuff 
has_one :expert
accepts_nested_attributes_for :expert, :update_only => true
// more stuff

Expert model - built withOUT devise.

class Expert < ActiveRecord::Base  
belongs_to :user

User controller:

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

Expert controller:

def edit
  @expert = Expert.find(params[:id])
  @title = "Edit Expert Profile"
end

Edit user view form partial:

<%= 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 %>

解决方案

In your UsersController, you should add :update to the authenticate_user! filter:

before_filter :authenticate_user!, :only => [:show, :edit, :update]

这篇关于Rails 4嵌套形式与设计记录我的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆