我是关于Ruby on Rails的新手......
我想在我的应用中,在我的会议页面上显示: 按钮"预订座位"如果尚未为会议预订current_user或显示信息"已预订"如果已经为会议预订了current_user。 (只有管理员才能创建会议)
我正在使用设计..
我知道这并不难,但我没有成功摆脱这个功能。
提前致谢。
这是我的代码:
meetings.html.erb:
<% @meetings.each do |meeting| %>
<div class="col-xs-12 col-sm-4">
<div class="panel panel-default box-shadow">
<div class="panel-body">
<div class="col-xs-12 text-center">
<strong><%= meeting.name %></strong>
<br><%= meeting.details %><hr>
</div>
<div class="col-xs-12 text-center">
<%= image_tag "place.png", heigth:15, width: 30 %> <strong><%= meeting.address %></strong><br>
<%= image_tag "date.png", heigth:20, width: 45 %> <%= meeting.start_time.strftime("%d/%m/%Y") %>
<hr>
<% if user_signed_in? %>
<% if meeting == current_user.meetings %>
Already booked
<% else %>
<%= link_to "Book a seat", meeting_participate_path(meeting), method: :post, class: 'btn btn-md btn-info' %><br>
<% end %>
<% else %>
<%- if controller_name != 'sessions' %>
<%= link_to "connectez-vous", new_session_path(resource_name), class:"btn btn-info" %><br />
<% end -%>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
MeetingController.rb:
def index
@meetings = Meeting.where('start_time > ?', Time.now)
end
def create
@meeting = current_user.meetings.create(meeting_params)
if @meeting.save
redirect_to meetings_path
else
render :new
end
end
def participate
meeting = Meeting.find(params[:meeting_id])
current_user.meetings << meeting
redirect_to meetings_path, alert: "Booked on the meeting"
end
private
def set_meeting
@meeting = Meeting.find(params[:id])
end
def meeting_params
params.require(:meeting).permit(:name, :details, :address, :start_time, :highlight, :user_id)
end
答案 0 :(得分:0)
您可以按照以下步骤
来实现这一目标在#=> Check if already added?
def meeting_already_added?(id)
meeting = Meeting.find(id)
return false unless meeting
user_meetings.where(meeting_id: meeting.id).exists?
end
#=> Pass id by following method
def can_book_meeting?(id)
!meeting_already_added?(id)
end
meetings.html.erb
在<% if user_signed_in? %> # Check user signed in?
<% if current_user.can_book_meeting?(meeting.id) %> # current_user added already? if not then...
<%= link_to "Book a seat", meeting_participate_path(meeting), method: :post, class: 'btn btn-md btn-info' %><br>
<% else %>
Already booked
<% end %>
<% else %> # Not signed in
Please SignIn
<% end %>
before_action :authenticate_user!, only: [:create, :participate]
通过在控制器顶部添加此行来为用户添加限制,该行需要对用户进行身份验证。
["3a67ef_V1", "3a67ef", "2b678_V1", "1b", "3a67ef_V2", "2b678_V2", "2b678"]