Rails 4 Nested form with devise logs me out on update

坚强是说给别人听的谎言 提交于 2019-12-11 11:37:25

问题


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

回答1:


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

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


来源:https://stackoverflow.com/questions/23013331/rails-4-nested-form-with-devise-logs-me-out-on-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!