我想在数据库中为用户的帖子创建一个条目,而无需重新加载页面。但在触摸时没有找到路线。
let_controller.rb
class LetController < ApplicationController
respond_to :html, :js
def index
end
def contacts
@contacts = Question.new
end
def language
session[:language] = params[:language][0..1]
set_language
redirect_to root_path
end
def new
@contacts = Question.new
end
def create
@contacts = Question.new(question_params)
@contacts.request = request
end
private
def question_params
params.require(:questions).permit(:name, :e_mail, :phone, :text_q )
end
end
contacts.html.erb
<div id="main-block">
<%= render 'contact_form' %>
</div>
_contact_form.html.erb
<ul class="errors"></ul>
<%= form_for @contacts , :remote => true do |f| %>
<%= f.text_field :name , placeholder: "Имя" %> <br>
<%= f.text_field :e_mail, placeholder: "e-mail" %><br>
<%= f.text_field :phone, placeholder: "Телефон" %><br>
<%= f.text_area :text_q, placeholder: "Сообщение" %><br>
<%= f.submit "Задать вопрос", class: "btn btn-primary"%>
<% end %>
save.js.erb
$("ul.errors").html("")
<% if @contacts.errors.any? %>
<% @contacts.errors.full_messages.each do |message| %>
$("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
<% end %>
<% else %>
$("#main-block").empty()
$("#main-block").html("Сообщение отправлено!")
<% end %>
create.js.erb
<%= render 'save' %>
new.js.erb
$("#main-block").html("<%= escape_javascript(render 'contact_form') %>")
的routes.rb
resources :let
root 'let#index'
match '/index', to: 'let#index', via: 'get'
match '/contacts', to: 'let#contacts', via: 'get'
post '/let/contacts', as: 'contacts'
终端加载页面
Started GET "/contacts" for ::1 at 2016-06-14 15:04:57 +0300
Processing by LetController#contacts as HTML
Rendered let/_contact_form.html.erb (0.7ms)
Rendered let/contacts.html.erb within layouts/application (2.4ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (4.3ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 70ms (Views: 69.0ms | ActiveRecord: 0.0ms)
单击按钮提交前终端
Started POST "/contacts" for ::1 at 2016-06-14 15:08:18 +0300
ActionController::RoutingError (No route matches [POST] "/contacts"):
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/Users/os/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/os/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/os/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
答案 0 :(得分:0)
您可以将路线重构为
resources :let do
collection do
get 'contacts', to 'let#contacts'
post 'contacts', to 'let#create', as: :create_contacts
end
end
root 'let#index'
并更改表单中的网址
<%= form_for @contacts, url: { action: :create }, remote: true do |f| %>