今、Ruby on Railsで、Userモデルに:name, :email, :password, :password_confirmation, :budgetの5つが入っています。
これら五つのうち、nameとemailとbudgetのみのテキストフィールドを表示させるページ( user/:id/edit )を作成して、form_forでupdateさせたいと思っています。( 残りのpasswordとpassword_confirmationはこのページでは変更を加えないということです )
そこで、以下のようなform_forを作り、controllerのupdateアクションにいかせようと思ったのですが、どうも正常にupdateされません。( updateのflash[:notice]側のelseにいってしまう )
原因はedit.html.erb内でpasswordとpassword_confirmationについて明示していないのでuser_paramsの値が
"user"=>{"name"=>"after_hoge", "email"=>"afterhoge@example.com", "budget"=>"10000"}
になってしまっているということがわかったのですが、それに対する対処法がわからないです。
どなたかご教授願えますでしょうか?
edit.html.erb
<div class="well col-md-6 col-md-offset-3"> <!--small>初めての方はアカウントを<%= link_to "新規作成", signup_path %>してください</small--> <%= form_for(@user) do |form| %> <div class="field"> <%= form.label :name %> <%= form.text_field :name, id: :user_name, class: 'form-control'%> </div> <div class="field"> <%= form.label :email %> <%= form.text_field :email, id: :user_email, class: 'form-control' %> </div> <div class="field"> <%= form.label :budget ,'今月の予算' %> <%= form.number_field :budget, id: :user_budget, class: 'form-control' %> </div> <!--div class="field"> <%= form.label :exp %> <%= form.number_field :exp, id: :user_exp %> </div> <div class="field"> <%= form.label :level %> <%= form.number_field :level, id: :user_level %> </div--> <div class="actions text-center"><br> <%= form.submit '登録', class: 'btn btn-primary' %> </div> <% end %>
updateアクション
def update respond_to do |format| if @user.update(user_params) flash[:success] = "User was successfully updated." format.html { redirect_to @user } format.json { render :show, status: :ok, location: @user } return redirect_to user_url @user else #puts(user_params) flash[:notice] = "User updating was failed." format.html { render :edit } format.json { render json: @user.errors, status: :unprocessable_entity } end end
[修正内容]
モデル側のバリデーションとのことで、user.rbとuser_test.rbを載せさせていただきます。
ちなみにreils testは通ってます(全然テスト書いていないのであたりまえかもしれないですが)
user.rb
class User < ApplicationRecord has_many :books, dependent: :destroy before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :budget, numericality: true validates :password, presence: true, length: { minimum: 6 } has_secure_password # 与えられた文字列のハッシュ値を返す def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end # ランダムなトークンを返す def User.new_token SecureRandom.urlsafe_base64 end end
user_test.rb
require 'test_helper' class UserTest < ActiveSupport::TestCase # test "the truth" do # assert true # end #@user.validはバリデーションを通るとture,invalidだと逆に通らないとtureになる def setup @user = User.new(name: 'foobar', email: 'sample@example.com', budget: 1000, exp:0, level:0, password:'foobar', password_confirmation: 'foobar') end #正しいユーザーはバリデーションを通る test "user validates all" do assert @user.valid? end #名前が50文字以上だと通らない test "user validates name" do @user.name = "a" * 51 assert @user.invalid? end #emailは正しいものを test "user validates email" do @user.email = "a" assert @user.invalid? end #budgetは整数で test "user validates budget" do @user.budget = "100a" assert @user.invalid? end #passwordは6文字以上 test "user validates password" do @user.password = "a" * 5 @user.password_confirmation = @user.password assert @user.invalid? end #passwodとpassword_confirmationは同じでなくてはいけない test "user validates passowrd = password_confirmation" do @user.password = "difference" assert @user.invalid? end #登録されているemailは使えない(user.ymlのoneがtest@sample.com) test "user validates email exists" do @user.email = "test@sample.com" assert @user.invalid? end end
回答1件
あなたの回答
tips
プレビュー