ddsbro
ddsbro

Reputation: 217

Rails button to disable if data is in database

I have a button that i need disabling if the data it was going to submit already exists in the database.

Bare in mind ive only been using this for like a month. so im a super noob!! haha!

The button is this <%= link_to 'Table', admin_table_statuses_path(table_id: @table.id), class: "btn btn-primary", method: :post, remote: true%>

my controller for this button is pretty basic also. see here

def create
    table_id = params[:table_id] #Keep the same
    @table_status = tableStatus.new
    @table_status.table_id = params[:table_id]
    @table_status.invoiced_out_user_id = current_user.id
    @table_status.invoiced_out_datetime = DateTime.now
    if @table_status.save
    # Success
      flash[:notice] = "Done!"
    else
      flash[:notice] = "Fail"
    end

Upvotes: 1

Views: 114

Answers (1)

Abhi
Abhi

Reputation: 4261

Give an ID to the button:

<%= link_to 'Table', admin_table_statuses_path(table_id: @table.id), class: "btn btn-primary", id: 'create_button',method: :post, remote: true%>

Inside create method do:

def create
.
.
.
  render :update do |page|
    if @table_status.save
      page << "document.getElementById('create_button').setAttribute('href', '')"
    end
  end
end

Since, the element is a link not button, you can just remove it's href, so that it doesn't hit create method again.

Upvotes: 1

Related Questions