我正在尝试从实验室控制器的索引操作将数据插入Subscriber。这是我的代码。
查看HTML:
class LabsController < ApplicationController
layout "labs"
def index
@subscriber = Subscriber.new
end
end
主控制器
class SubscribersController < ApplicationController
def create
@subscriber=Subscriber.new(subscriber_params)
if @subscriber.save
redirect_to root_path, notice: "Subscribed Successfully !"
else
redirect_to root_path, notice: "Subscription Failed !"
end
end
private
def subscriber_params
params.require(:subscriber).permit(:email_address)
end
end
订户控制器
Rails.application.routes.draw do
resources :subscribers
root 'labs#index'
get "home", :to => "labs#index"
end
的routes.rb
Started GET "/home?subscriber%5Bemail_address%5D=ss%40w&commit=Subscribe" for 127.0.0.1 at 2018-06-06 23:19:57 +0530
Processing by LabsController#index as HTML
Parameters: {"subscriber"=>{"email_address"=>"ss@w"}, "commit"=>"Subscribe"}
Rendering labs/index.html.erb within layouts/labs
Rendered labs/index.html.erb within layouts/labs (1.6ms)
Completed 200 OK in 19ms (Views: 18.0ms | ActiveRecord: 0.0ms)
控制台中的输出
{{1}}
答案 0 :(得分:1)
不要将form_for
包裹在<form></form>
标记内。当你点击&#34;提交&#34;它返回标记的简单GET请求而不是form_for
rails helper(已禁用)。
<div class="container">
<%= form_for @subscriber, url: url_for(controller: "subscribers", action: "create" ), remote: true, html: { id: 'login' } do |f| %>
<%= f.label :name, 'SUBSCRIBE TO OUR NEWSLETTER' %>
<div id="subscribe-text">
<%= f.email_field :email_address, placeholder: "email address",row: "3", class: 'email_class' %>
</div>
<%= f.submit "Subscribe", class: 'button_class' %>
<% end %>
</div>