0

I am trying to insert data into Subscriber from index action of labs controller. This is my code.

View HTML:

<div class="container">
  <form id="login">
    <div class="subscribe">
         <%= form_for @subscriber, :url => url_for(:controller => "subscribers", :action => "create" ), remote: true do |f| %>
            <%= f.label :name, 'SUBSCRIBE TO OUR NEWSLETTER' %>&nbsp;&nbsp;&nbsp;
                    <div id="subscribe-text">
            <%= f.email_field :email_address,placeholder: "email address",row:"3", :class => 'email_class' %>&nbsp;&nbsp;&nbsp;
          </div>
        <%= f.submit "Subscribe", :class => 'button_class' %>
              <% end %>
      </form>
      </div>

            </div>

Main Controller

class LabsController < ApplicationController
  layout "labs"
  def index
    @subscriber = Subscriber.new
  end
end

Subscriber Controller

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

routes.rb

Rails.application.routes.draw do
  resources :subscribers
  root 'labs#index'
  get "home", :to => "labs#index"
end

Output in the console

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)
2
  • If you inspect the form in your browser what is the URL is going to, and what method? Commented Jun 6, 2018 at 18:42
  • Its going to /subscribers with POST method Commented Jun 6, 2018 at 23:33

1 Answer 1

1

Don't wrap the form_for inside a <form></form> markup. When you hit "Submit" it returns a simple GET request for the markup not the form_for rails helper (which is disabled).

<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' %>&nbsp;&nbsp;&nbsp;
    <div id="subscribe-text">
      <%= f.email_field :email_address, placeholder: "email address",row: "3", class: 'email_class' %>&nbsp;&nbsp;&nbsp;
    </div>
    <%= f.submit "Subscribe", class: 'button_class' %>
  <% end %>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

You are awesome. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.