ruby-on-railsruby-on-rails-4button-to

Rails, check condition when user click button_to


I'm doing an application for booking movie. For each movie have a "book" button, normally it just direct to another page handled by a controller. But is there any way to do like this? if user not login before, show a popup to redirect to login page, else direct to booking controller?

Current in my movie controller is:

<td><%= button_to "Book", {:controller => "booking",:action=>"new", :movie_session_id => movie.id}, :method => :post %></td>

is it possible if I check condition in this page or I have to check in BookingController?


Solution

  • You can try the following-

    <td>
    <% if current_user %>
    
        <%= link_to "Book", {:controller => "booking",:action=>"new", :movie_session_id => movie.id}, :method => :post %>
    <% else %>
      <%= link_to "Book", login_path %>
    <% end %>
    </td>