Rails, check condition when user click button_to

1.5k Views Asked by At

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?

2

There are 2 best solutions below

0
dp7 On BEST ANSWER

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>
0
Vishal On

link_to_if(current_user.blank?, "Book", login_path) do

link_to "Book", {:controller => "booking",:action=>"new", :movie_session_id => movie.id, :method => :post}

end

if the user is logged in than it redirect to login page else it will redirect to booking new form