我是Ruby on Rails的新手,並且遇到了在窗體上不顯示錯誤消息的問題。我一直堅持這幾天,我已經嘗試了幾乎所有的解決方案/建議,我已經看到這個錯誤,但我一直無法使它工作。我必須形成一個爲用戶和一個爲綠色。它們都使用部分相同的共享消息,並且當輸入無效時,用戶窗體顯示錯誤消息,但綠色窗體不顯示錯誤消息。字段無效時不顯示共享的錯誤消息
綠色形式(在greenmetrics顯示查看):
<%=form_for(@green) do |f| %>
<%= render partial: 'shared/error_messages', object: f.object %>
<div class="form-group">
<div class="col-xs-2"><legend> Add new metrics:</legend></div>
<div class="col-xs-2 ">
<%= f.number_field :number, class: 'form-control', placeholder: "Green number" %>
</div>
</div>
<div class="form-group">
<div class="col-xs-2 ">
<%= f.number_field :speed, class: 'form-control', placeholder: "Green speed", :step => 'any' %>
</div>
</div>
<div class="form-group">
<div class="col-xs-2 ">
<%= f.number_field :firmness, class: 'form-control', placeholder: "Green firmness", :step => 'any' %>
</div>
</div>
<div class="form-group">
<div class="col-xs-2 ">
<%= f.date_field :measure_date, class: 'form-control', placeholder: "Date of measurement" %>
</div>
</div
<div class="form-group">
<div class="col-xs-1 ">
<%= f.submit "Submit", class: "btn btn-primary" %>
</div>
</div
</div>
<% end %>
用戶形成:
<% provide(:title, 'Register') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :first_name %>
<%= f.text_field :firstname, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :lastname, class: 'form-control' %>
<%= f.label :golf_course %>
<%= f.text_field :golf_course, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :username %>
<%= f.text_field :username, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
共用消息偏
綠色模型
class Green < ActiveRecord::Base
belongs_to :user
default_scope -> { order(measure_date: :desc, number: :asc) }
validates :user_id, presence: true
validates :number, presence: true
validates :speed, presence: true
validates :firmness, presence: true
validates :measure_date, presence: true
end
用戶模型:
class User < ActiveRecord::Base
has_many :greens, dependent: :destroy
before_save { self.username = username.downcase}
before_save { self.email = email.downcase}
validates :firstname, presence: true, length: { maximum: 50 }
validates :lastname, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
#,uniqueness: { case_sensitive: false}
validates :golf_course, presence: true, length: { maximum: 50 }
validates :username, presence: true, length: { maximum: 25 },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, length: {minimum: 6}
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
Controller
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@greens = @user.greens
end
def greenmetrics
@user = current_user
@greens = @user.greens
@green = current_user.greens.build if logged_in?
end
綠黨控制器:
def create
@user = current_user
@green = current_user.greens.build(green_params)
if @green.save
flash[:success] = "Green metrics saved!"
@greens = @user.greens
redirect_to (:back)
else
flash[:danger] = "Please verify that all data is valid"
redirect_to (:back)
end
end
我因爲沒有得到從顯示驗證錯誤消息添加閃光危險......
在此先感謝您的幫助。
安德烈
用戶形式也是一個新的用戶,它顯示了所有無效的領域,如果你離開他們的黑色,不輸入有效錯誤等。所有驗證都是模型的一部分,並要求所有字段都存在並符合其類型。有什麼想法嗎? – Andrea
用戶表單從操作中獲取其用戶。綠色表格*總是創建一個新的綠色*。 –
我試過把它作爲變量傳遞給它,和@user一樣,但仍然不顯示消息。我已經添加了控制器定義 – Andrea