模型验证方法从服务器轨道5返回错误

时间:2017-10-04 05:53:37

标签: ruby-on-rails ruby

我在Set IE = CreateObject("InternetExplorer.Application") IE.Visible = 1 IE.navigate "www.webpage.com" WScript.Sleep 10000 Set Var = IE.document.getElementByName("user").value = "myusername" Set Var = IE.document.getElementByID("password").value = "mypassword" Set Var = IE.document.Forms(0) Var.Submit 中有这个方法,当我运行这个方法时,错误很明显。当我提交表单时,我不会从模型中获取错误,而是在控制器中呈现micropost.rb条件。 模型中的某些内容似乎有问题,但不知道为什么我会从服务器获得回滚事务?

该方法只检查用户是否未输入并发送多个字段。

Micropost.rb

else

MicropostController

validate :return_media_field_errors

private 

def return_media_field_errors
  if :img_url? && :video_url? 
    errors.add(:img_url, "Can only submit one field at a time")
  end
end

micropost schema.rb

def create
  @micropost = current_user.microposts.build(micropost_params)
  if @micropost.save
    flash[:success] = "Micropost created!"
    @micropost  = current_user.microposts.build
    @feed_items = current_user.feed.paginate(page: params[:page])
    render 'static_pages/home'
  else
    @feed_items = []
    render 'shared/error_messages'
  end
end

private

  def micropost_params
    params.require(:micropost).permit(:content, :picture, :picture_cache, :image_url, :video_url, :gif_url)
  end

查看参数

create_table "microposts", force: :cascade do |t|
  t.text     "content"
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string   "picture"
  t.string   "image_url"
  t.string   "video_url"
  t.string   "gif_url"
  t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
  t.index ["user_id"], name: "index_microposts_on_user_id"
end

1 个答案:

答案 0 :(得分:1)

我认为问题在于这一行:

if :img_url? && :video_url? 

此处:img_url?是符号,而不是属性

您想要的是self.image_url?image_url,同样适用于video_url