【问题标题】:Rails and ValidationRails 和验证
【发布时间】:2013-09-03 15:35:55
【问题描述】:

我仍然(对于那些以前帮助过的人)在验证数据时遇到问题。

情景

我有两个模型 - useraccommodation,每个用户都有一个住宿 (has_one)。我可以使用 current_user 访问已登录的用户。

问题

当我在注册时验证用户时,一切正常,并为每个验证规则显示相应的验证错误消息。但是,我现在正在尝试在输入住宿时验证住宿,但出现 Rails 错误页面:

未知操作

无法为住宿控制器找到“显示”操作

但有趣的是,url 已更改为 /accommodations//edit,这似乎缺少住宿 id 的 id(如果一切正常,我确实希望它转移到编辑)。

我认为这不是验证规则本身,而是我如何处理重定向(老实说这让我感到困惑!)。如果通过验证规则,数据会正确保存并正确重定向,但不确定如何优雅地处理“未保存”。

守则

住宿模式

class Accommodation < ActiveRecord::Base
  belongs_to :user

  #validation rules
  validates :user_id, presence: true
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: {    case_sensitive: false }
end

/accommodations/new.html.erb

...
<%= form_for :accommodation, url: accommodations_path do |f| %>

<% if @accommodation.errors.any? %>
  <% @accommodation.errors.full_messages.each do |msg| %>
    <p class="error"><%= msg %></p>
  <% end %>
<% end %>

...

AccommodationsController(感谢@depa 提供的帮助)

...
def index
  if current_user.accommodation.present?
    redirect_to edit_accommodation_path(current_user.accommodation)
  else
    redirect_to new_accommodation_path(current_user.accommodation)
  end
end

def new
  @accommodation = Accommodation.new
end

def create
  current_user.create_accommodation(accommodation_params)
  flash[:success] = "Accommodation details added"
  redirect_to edit_accommodation_path(current_user.accommodation)
end

def edit
end

def update 
  if @accommodation.update(accommodation_params)
    flash[:success] = "Accommodation details updated successfully"
    redirect_to edit_accommodation_path(@accommodation)
  else
    flash[:error] = "Accommodation details could not be updated"
    render 'edit'
  end
end

private
  def accommodation_params
    params.require(:accommodation).permit(:name, :email)
  end
...

【问题讨论】:

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


    【解决方案1】:

    优雅地处理失败的验证:

    def create
      @accommodation = current_user.build_accommodation(accommodation_params)
      if @accommodation.save
        flash[:success] = "Accommodation details added"
        redirect_to edit_accommodation_path(current_user.accommodation)
      else
        flash.now.notice = "Error creating accommodation"
        render "new"
      end
    end
    

    【讨论】:

    • 感谢您的帮助,但效果不佳 当我故意输入无效数据时,我得到另一个 Rails 错误页面,例如“验证失败:电子邮件无效”,它突出显示了 accom.save!行
    • 应该是.save 而不是.save!,我的错。将更新答案。
    • 好的 - 但现在我又回到了我之前遇到的问题 - “NoMethodError in Accommodations#create”和“nil:NilClass 的未定义方法‘错误’”突出显示我的代码以显示验证错误。我需要提供住宿对象吗?
    • 是的,您的编辑操作应该是@accomodation = Accomodation.find(params[:id)。还修改了答案。
    • 这太棒了 - 非常感谢。可能想要更新您的住宿拼写,但以防其他人最终复制和粘贴(出于某种奇怪的原因!)哈哈。我将您的答案标记为正确。
    【解决方案2】:

    accommodation_params 是否真的在进入create 时设置?我希望您需要使用paramsparams[:accommodation]

    如果不是,create_accommodation 调用将失败,这意味着current_user.accommodation 将是nil,这很可能会产生您的错误。

    【讨论】:

    • 但请记住 - 当我添加有效数据时,它会成功添加到数据库中 - 所以数据肯定会被传递
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多