这可能是显而易见的,但我需要另外一双眼睛来获得它。单击我的“提交”按钮不会产生任何结果(不是重定向,只是没有)。
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
书籍/ 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 %>
正如我之前所说,当我点击提交按钮时没有任何反应。有没有人有想法?
提前致谢。
答案 0 :(得分:1)
感谢大家的帮助,但这是由于JS捕获表单提交事件。不是Ruby问题。
我将结束这个话题。
感谢。
答案 1 :(得分:0)
确定第一步,你不需要写url,rails可以推断它。所以
<%= form_for Book.new 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 %>
第2步,最重要的是,你没有保存它
def create
@book = Book.new params[:book]
@book.save!
render "index"
end
或
def create
@book = Book.create! params[:book]
render "index"
end