我正在構建一個事件應用程序,該應用程序在視圖中使用簡單窗體來創建事件。我試圖實施驗證,以便某些細節必須存在。當我測試了這個 - 通過故意丟失表單的各個部分 - 我遇到了大量的問題/錯誤/錯誤,拋出了。唯一重新恢復正常的方法是我刪除'有毒「的事件通過控制檯。Rails錯誤 - 應用程序未選取驗證和/或防錯代碼塊
就好像視圖中的驗證和錯誤代碼塊沒有任何作用,事件仍在創建並分配了一個id,並且代碼剛剛打破,創建了一個bug,當我故意忽略表單的某些部分。
不知道爲什麼會發生這種情況。這是我的相關代碼 -
EventsController.rb
class EventsController < ApplicationController
before_action :find_event, only: [:show, :edit, :update, :destroy,]
# the before_actions will take care of finding the correct event for us
# this ties in with the private method below
before_action :authenticate_user!, except: [:index, :show]
# this ensures only users who are signed in can alter an event
def new
@event = current_user.events.build
# this now builds out from a user once devise gem is added
# after initially having an argument of Event.new
# this assigns events to users
end
def create
@event = current_user.events.build(event_params)
# as above this now assigns events to users
# rather than Event.new
if @event.save
redirect_to @event, notice: "Congratulations, you have successfully created a new event."
else
render 'new'
end
end
private
def event_params
params.require(:event).permit(:title, :location, :date, :time, :description, :number_of_spaces, :is_free, :price, :organised_by, :url, :image, :category_id)
# category_id added at the end to ensure this is assigned to each new event created
end
def find_event
@event = Event.find(params[:id])
end
end
這是我的表單部分它有一個錯誤代碼塊 - 這個最初扔了一個錯誤,指出預計2
時正在傳遞0參數_form.events.html.erb
<%= simple_form_for(@event) do |f| %>
<% if @event.errors.any? %>
<h2><%= pluralize(@event.errors.count, "error") %> prevented this Event from saving:</h2>
<ul>
<% @event.errors.full_message.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
<!-- The above code loop assigns a category_id to each event -->
<%= f.input :image, as: :file, label: 'Image' %>
<%= f.input :title, label: 'Event Title' %>
<label>Location</label><%= f.text_field :location, id: 'geocomplete' %></br>
<label>Date</label><%= f.text_field :date, label: 'Date', id: 'datepicker' %>
<%= f.input :time, label: 'Time' %>
<%= f.input :description, label: 'Description' %>
<label>Number of spaces available</label><%= f.text_field :number_of_spaces, label: 'Number of spaces' %>
<%= f.input :is_free, label: 'Tick box if Event is free of charge' %>
<!--f.input :currency, :collection => [['£GBP - British Pounds',1],['$USD - US Dollars',2],['€EUR - Euros',3]] -->
<%= f.input :price, label: 'Cost per person (leave blank if free of charge)' %>
<%= f.input :organised_by, label: 'Organised by' %>
<%= f.input :url, label: "Link to Organiser site" %>
<%= f.button :submit, label: 'Submit' %>
<% end %>
與驗證事件模型。無論是否包含驗證,都會發生錯誤(我已將它們取出並嘗試使用相同的結果)。
Event.rb
class Event < ActiveRecord::Base
belongs_to :category
belongs_to :user
has_many :bookings
has_many :comments
has_attached_file :image, styles: { medium: "300x300>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_associated :category
validates :title, :description, :location, :date, :time, :number_of_spaces, :price_pennies, presence: true
monetize :price_pennies
# required for money-rails gem to function
end
的消息似乎在從「的ActionController :: InvalidAuthenticityToken」的地方,以「未定義的方法」的所有反彈一行的代碼在我的節目頁面錯誤 -
<p><%= @event.date.strftime('%A, %d %b %Y') %></p>
當我試圖離開日期字段爲空。
在你看來
您可以顯示一些錯誤信息,並留下了代碼是不相關的問題嗎? (如控制器顯示/索引/編輯/更新/銷燬)。 關於真實性標記:當您提交表單時,它是否存在於HTML中。它是否提交給控制器? –
如何在此處顯示錯誤代碼?我已通過在控制檯中刪除事件修復了錯誤,但知道它會再次發生。有關與事件控制器中的創建操作相關的真實性標記的錯誤。我會更新我的回答,並提供您對相關代碼的評論。 –
我得到的第一個錯誤是 - –