Ruby on Rails:为特定时间戳定义一个变量,该变量可由应用程序用户编辑



我的目标是为某个时间戳定义一个变量(@deadline),应用程序用户可以对其进行编辑。我有办法通过创建一个显示视图来解决这个问题,该视图为用户提供了一个表单。

<%= link_to "Anmeldefrist setzen", show_deadline_path(@deadline), :method => 'get' %>

该显示视图可以访问并按照我想要的方式工作,当我按下按钮提交时间戳时,斗争就开始了。

<% if logged_in? and current_user.admin? %>
<% provide(:title, "Anmeldefrist setzen") %>
<div class="small_jumbotron jumbotron">
<h1> Anmeldefrist setzen </h1>
<div class="row">
<div class="Links">
<%= form_tag(:controller => "preferences", :action  => "update_deadline", :method => :put) do %>
<%= label @deadline, "Anmeldefrist" %>
<br>
<%= datetime_select @deadline, value: @deadline %>
<br>
<%= submit_tag "Speichern", class: "btn btn-primary" %>
<br>
<% end %>
<h2> <%= link_to image_tag('back2.png'), matching_path %>
</div>
</div>
</div>
<% end %>

路由文件不接受表单中的"put"方法。我之前也尝试过使用"补丁"来做到这一点。我总是收到路由错误。

No route matches [POST] "/update_deadline"

我现在将添加日志文件,routes.rb和控制器,感谢所有尝试提供帮助的人!你也可以批评我的方法并提出另一种方法,但我会欣赏详细的说明。

谢谢!亲切问候 莫里茨

日志: 在 2018-03-07 16:45:48 +0100 开始 127.0.0.1 的 GET "/show_deadline" 由 PreferencesController#show_deadline 作为 HTML 处理 在布局/应用程序中呈现首选项/show_deadline.html.erb [1m[36m用户负载(0.2ms)[0m [1m[34m选择"用户".*从"用户"到"用户"的地方。id" = ?限制?[0m [["id", 1], ["限制", 1]] 在布局/应用程序中呈现首选项/show_deadline.html.erb (20.8ms) 渲染布局/_rails_default.html.erb (131.7ms) 渲染布局/_shim.html.erb (0.8ms) 渲染布局/_header.html.erb (4.6ms) 渲染布局/_footer.html.erb (0.8ms) 在 183ms 内完成 200 OK (浏览: 180.2ms |活动记录:0.2毫秒)

Started POST "/update_deadline?method=put" for 127.0.0.1 at 2018-03-07 16:45:54 +0100
ActionController::RoutingError (No route matches [POST] "/update_deadline"):
actionpack (5.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:63:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.2) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.2) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.2) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.2) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.2) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.2) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.2) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.2) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

路线:

Rails.application.routes.draw do
resources :preferences
resources :institutes
resources :users
resources :admin, to: 'users#admin'
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get '/home', to: 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/matching', to: 'static_pages#matching'
get '/cockpit', to: 'static_pages#cockpit'
get '/signup', to: 'users#new'
post '/signup',  to: 'users#create'
get '/performance_show', to: 'users#performance_show'
get '/performance_update', to: 'users#performance_update'
post 'preferences/create_all', :to => 'preferences#create_all'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets,     only: [:new, :create, :edit, :update]
get '/show_deadline', :to => 'preferences#show_deadline'
patch '/update_deadline', :to => 'preferences#update_deadline'
post 'preferences/delete_matching', :to => 'preferences#delete_matching'
post 'preferences/read_and_show_ofv', :to => 'preferences#read_and_show_ofv'
post 'preferences/read_matching', :to => 'preferences#read_matching'
post 'preferences/optimize_matching', :to => 'preferences#optimize_matching'
post 'preferences/optimize', to: 'preferences#optimize'
end

控制器:

def show_deadline
if @deadline.nil?
@deadline = 0
end
end
def update_deadline
if @deadline.update_attributes
flash[:success] = "Die Deadline wurde aktualisiert."
redirect_to matching_path
else
flash[:error] = "Die Präferenz konnte nicht aktualisiert werden."
redirect_to preferences_path
end
end

您需要将实例变量保留在某个地方,否则在完成请求后,它将消失在稀薄的空气中 - 某个地方也可以是一个模型。

创建迁移并将日期时间字段添加到模型中:

rails g migration add_deadline_to_preferences deadline:datetime

添加验证以确保时间在将来:

class Preference < ApplicationRecord
validate :deadline_is_not_elapsed
private 
def deadline_is_not_elapsed
errors.add(:deadline, 'Deadline has elapsed') if deadline > Time.now
end
end

将属性添加到表单中:

<%= form_with(model: @preference || Preference.new) do |f| %>
<div class="field">
<%= f.label :deadline %>
<%= f.datetime_select :deadline, start_year: Time.now.year, default: 3.days.from_now  %>
</div>
# ...
<% end %>

并将其列入白名单:

def preference_attributes
params.require(:preference).permit(:foo, :bar, :deadline)
end

最新更新