Rails 4:无法更新当前用户

时间:2016-11-22 22:53:57

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试更新current_user的某些特定值,但我没有运气。这些值不会保存,并且页面不会被重定向到任何地方。这是更新中用户控制器的代码

def update

    @user = current_user

    respond_to do |format|
      if @user.update_attributes(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit, notice: 'Update unsuccessful' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end


def user_params
      params.require(:user).permit(:username, :password, :password_confirmation, :email, :weight, :height, :age, :activity_level, :goal, :calorie_goal, :fat_goal, :carb_goal, :protein_goal, :fiber_goal, :sugar_goal)
    end

我尝试使用params [:user]或params [:current_user]而不是user_params但没有运气..这里是html

<h2>Set Up Your Profile</h2>

<% @user = current_user %>

<%= form_for(@user) do |f| %>

  current user is <%= current_user.username %>

  <div class="field">
    <%= f.label :weight %><br>
    <%= f.text_field :weight %>
  </div>
  <div class="field">
    <%= f.label :height %><br>
    <%= f.text_field :height %>
  </div>
  <div class="field">
    <%= f.label :age %><br>
    <%= f.number_field :age %>
  </div>
  <div class="field">
    <%= f.label :activity_level %><br>
    <%= f.number_field :activity_level %>
  </div>
  <div class="field">
    <%= f.label :goal %><br>
    <%= f.number_field :goal %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

这里我尝试了form_for(current_user),但也没有用......有什么想法在这里有什么问题?

0 个答案:

没有答案