Sam Roberts
Sam Roberts

Reputation: 185

Rails validation to make something not editable if live

I'm trying to get my head around rails validation. So far i have this

  def live
    person.live
  end

However i have no idea what to do after this, The users have a button to click to make there person live. Which changes a value in the DB from false to true.

they then have a link to be able to edit the user which is here

/people/{{person.id}}/edit

How do i make it so the users cannot access the link (via typing it into the browser) after the event is live?

Thanks

Upvotes: 1

Views: 159

Answers (2)

Vucko
Vucko

Reputation: 20844

"How do i make it so the users cannot access the link (via typing it into the browser) after the event is live?"

You can use before_action filter to check if live field is true. Something like:

Controller:

before_action :check_if_live_true

private

def check_if_live_true
    if live_field.true?
        redirect_to some_path
    end
end

Which will redirect if the live field is true, then someone enters the matching URL.

Upvotes: 1

Klante
Klante

Reputation: 141

I would recommend using some kind of authorization gem. Personally i use CanCanCan.

There you can specifiy what a user can access and not and where it should redirect to etc.

In Ability.rb specify this

can :update, Person, :live => false

In Controllers use this

authorize! :edit, @person

In views you can then use something like this

<% if can? :update, @person%>
  <%= link_to "Edit", edit_person_path(@person) %>
<% end %>

Upvotes: 2

Related Questions