简体   繁体   中英

Rails, Check if url already exists

I have a simple_form that has a bunch of urls that need to be inputted into the database.

Now i'm wanting to have a new record matches existing record page where you can compare the issues?

For instance. If a record has google.com/search/stackoverflow

And if someone types in the same url to be inputted into the same column name. It will alert the user stating that the field already exists and display the ID number of the field that exists. It then wont save the new field and will redirect the user back to the dashboard.

Heres the code i have at the moment.

Controller:

  def create
    create_params = params[:newevent].permit(:eventname, :eventshortdesc, :eventvenuename, :eventdesc, :eventdatetime, :eventimage, :1link, :2link, :3link, :4link, :5link, :6link, :event_type, :eventready, :eventcomplete)
    @newevent = Newevent.new(create_params)
    @newevent.save!
  end

view

<%= f.input :eventname, label: "Event Name", required: true %>
  <%= f.input :event_type %>
  <%= f.input :eventdesc, label: "Description", required: true  %>
  <%= f.input :eventshortdesc, label: "Short Description", required: true  %>
  <%= f.input :eventvenuename, label: "Venue Name / Location", required: true  %>
  <%= f.input :eventdatetime, type: "datetime", label: "Date Of Event: (Enter as YYYY-MM-DD)", required: true %>
  <%= f.input :eventimage, :label => "Image Name Here (exact)"%>
  <%= f.input :1link, label: "1 URL of Event" %>
  <%= f.input :2link, label: "2 URL of Event" %>
  <%= f.input :3link, label: "3 URL of Event" %>
  <%= f.input :4link, label: "4 URL of Event" %>
  <%= f.input :5link, label: "5 URL of Event" %>
  <%= f.input :6link, label: "6 URL of Event" %>

This is all i have at the moment.

Thanks for helping if you can.

Sam

You just need a simple uniqueness validation.

class NewEvent < ActiveRecord::Base
  validate_uniqueness_of :1link, :2link, :3link # whatever attribute you want to validate
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM