简体   繁体   English

在 ruby​​ on rails 如何允许用户仅在周日(一天)创建帖子 工作日的其余时间不允许创建帖子

[英]In ruby on rails How to allow users to Create a post only Sunday(one day) rest of the weekday make create post not allow

we have 7 days in a week but users allow to create a post on only one day which is Sunday, how can I achieve that.我们一周有 7 天,但用户只允许在星期日的一天创建帖子,我该如何实现。 pls need help i am new to rails请帮助我是新来的Rails

in teams/controller在团队/控制器中

class TeamsController < ApplicationController
before_action :find_team_member, only: [:show, :edit, :update, :destroy]

def index
    @teams = Team.all
end 

def new
    @team = Team.new 
end

 def create
    @team = Team.create(team_params)
    @team.save
    params[:team][:team_employees].each do |emp_id| 
       @team.team_employees.create(employee_id: emp_id) 
    end
    #@team.employee_ids = params[:team][:team_employees]
    redirect_to teams_path
 end

private
  
 def find_tem_member
    @team = Team.find(params[:id])
 end

 def team_params
    params.require(:team).permit(:employee_id,:name,:restaurant_name)
 end
end

in teams/view在团队/视图中

<%= form_with model: @team, local: true do |f| %>
    <div class="field">
    <div class="field">
    <%= f.label "Add Leader"%>
   <%#= f.select :employee_id, Employee.where(is_leader: true).pluck(:name)%>
<%= f.collection_select :employee_id, Employee.where(is_leader: true),:id,:name, include_blank: true %>
<%= f.text_field :name%>
<%= f.text_field :restaurant_name%>
    </div></br>
    <div class="field">
    <%= f.label "Add users to team" %><br />
    <%= f.collection_check_boxes :team_employees, Employee.where(is_leader: false), :id, :name do |b| %>
        <div class="collection-check-box">
          <%= b.check_box %>
          <%= b.label %>
        </div>
     </div>   
  <% end %>
  
  </br>
    <%= f.button :submit%>

  <% end %>

in Team/model在团队/模型中

class Team < ApplicationRecord
has_many :team_employees
has_many :employees, through: :team_employees
belongs_to :employee
end
  1. we have 7 days in a week but users allow to create a post on only one day which is Sunday, how can I achieve that.我们一周有 7 天,但用户只允许在星期日的一天创建帖子,我该如何实现。 pls need help i am new to rails请帮助我是新来的Rails

You can use plain Ruby to check the current date/time, and halt the request if it's not Sunday.您可以使用纯 Ruby 检查当前日期/时间,如果不是星期天,则停止请求。 For example, you can use the Time#sunday?例如,您可以使用Time#sunday? method:方法:

require "time"

Time.now().sunday?
# => true or false

Time.parse("2022-07-09 11:10 UTC").sunday?
# => false

Time.parse("2022-07-10 11:10 UTC").sunday?
# => true

In Rails, you might also want to check the timezone of the user.在 Rails 中,您可能还想检查用户的时区。 A certain point in time could be Sunday in some place and Saturday or Monday somewhere else.某个时间点可能在某个地方是星期日,而在其他地方可能是星期六或星期一。 For example:例如:

timestamp = 1657494000

Time.at(timestamp, in: "+02:00").sunday?
# => false

Time.at(timestamp, in: "-03:00").sunday?
# => true

Rails provides its own advanced API to represent and work with timezones, but then how to determine the timezone of the current user is up to you. Rails 提供了自己的高级 API来表示和处理时区,但是如何确定当前用户的时区取决于您。 For simplicity, let's assume that you either want to use the server timezone or that you have something in app setup that sets it automatically.为简单起见,我们假设您想要使用服务器时区,或​​者您在应用程序设置中有自动设置它的东西。 Therefore, using Rails' Time.zone.now() should be better than plain Ruby's Time.now() , although the returned ActiveSupport::TimeWithZone instance must be converted to a plain Time instance in order to call the #sunday?因此,使用 Rails 的Time.zone.now()应该比普通的 Ruby 的Time.now()更好,尽管返回的ActiveSupport::TimeWithZone实例必须转换为普通的Time实例才能调用#sunday? method.方法。

Bringing everything together, your controller's action could include this:将所有内容放在一起,您的控制器的操作可能包括:

def create
  current_time = Time.zone.now.time
  unless current_time.sunday?
    flash[:error] = "You can't create a team unless it's Sunday"
    redirect_to teams_path
    return
  end

  @team = Team.create(team_params)
  # ... the rest of your logic
end

PS: if you invoke @team = Team.create(team_params) , that already inserts the record. PS:如果你调用@team = Team.create(team_params) ,那已经插入了记录。 You don't need to also call @team.save .您不需要也调用@team.save

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM