【发布时间】:2014-02-15 23:53:53
【问题描述】:
这可能很明显,但我需要另一双眼睛才能明白。点击我的“提交”按钮不会产生任何东西(不是重定向,只是什么都没有)。
Rails 4 应用是机场附近停车场的预订系统。一切都在这里:
routes.rb:
Parkix::Application.routes.draw do
...
resources :books
....
root to: "books#index"
end
books_controller.rb:
class BooksController < ApplicationController
def index
@book = Book.new
end
def create
@book = Book.new params[:book]
render "index"
end
end
book.rb(模型):
class Book < ActiveRecord::Base
attr_accessor :airport, :start_date, :start_time, :end_date, :end_time
belongs_to :park
belongs_to :user
has_many :prices, through: :parks
before_validation :generate_dates
private
def generate_dates
self.start = Time.strptime "#{self.start_date} #{self.start_time}", "%d/%m/%Y %H:%M"
self.end = Time.strptime "#{self.end_date} #{self.end_time}", "%d/%m/%Y %H:%M"
end
end
books/index.html.erb:
<%= form_for Book.new, url: { action: "create" }, method: :post do |f| %>
<%= f.text_field :airport, placeholder: "Airport" %>
<%= f.text_field :start_date, placeholder: "Start date" %>
<%= f.text_field :start_time, value: "10:00", placeholder: "Start time" %>
<%= f.text_field :end_date, placeholder: "End date" %>
<%= f.text_field :end_time, value: "10:00", placeholder: "End time" %>
<%= f.text_field :count_people, placeholder: "Count people" %>
<%= f.submit "Search", class: "btn btn-primary" %>
<% end %>
正如我之前所说,当我单击提交按钮时,什么也没有发生。有人有想法吗?
提前致谢。
【问题讨论】:
-
您的提交事件可能被 javascript 捕获。你试过禁用javascript吗?否则,我会查看 HTML5 验证错误...
-
你是对的 Julien,这是由于 JS 捕获了表单提交事件。问题解决了。
标签: ruby-on-rails-4