Wednesday, 4 June 2014

USER DEFINED FORMS AND FORM ELEMENTS ACCESSING

USER DEFINED FORMS AND ACCESS FORM ELEMENTS



In my Pages controller--->home.html.erb

I am creating another user defined form in home.html.erb:

    <%= form_tag( { :controller=>"newsletters", :action => :create, }, { :method => :post })  %>
                        <%= text_field_tag 'name','',:placeholder =>"Your FullName" %>
                        <%= text_field_tag 'email','',:placeholder => "Email Address" %>
    <%=submit_tag 'SUBMIT', :class=>"button-type1"%>

The controller goes to newsletters/create method.

newsletters_controller.rb

 def create
    @newsletter = Newsletter.new
#Accessing user defined elements in newsletter_controller.rb
    @newsletter.name=params[:name].to_s
    @newsletter.email=params[:email].to_s
    if @newsletter.save
# email sending statement
       NewsletterMailer.newsletter_confirmation(@newsletter).deliver
#For redirection to root path
       redirect_to :root, notice: 'Confirmation Mail Sent to Your Mail.'
    else
      render action: 'new'
    end

  end

No comments:

Post a Comment