THE SUBMIT BUTTON - submit

Udemy: Bootcamp course: section: 8 Lecture: 122: SAVING TO THE DATABSE: In the "Contact Us" page I filled in the info, Name: test1, Email: test1#example.com, Comments: test1, as a test, pressed submit, but the button do not submit the info and gives no error message. Where am I going wrong? Any expert advice? Here is my code:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
flash[:success] = 'Message sent.'
redirect_to new_contact_path
else
flash[:danger] = 'Error occured, message has not been sent.'
redirect_to new_contact_path
end
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well"
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>

Im not a rails expert but you can try <%= form_for(#contact, :method => :get) do |f| %> that should get it to work.
In your contact.rb model type the following
class Contact < ActiveRecord::Base
end

Related

Contact name not showing name in DB

I'm a rookie, and looking to create a contact form, but when I check my submitted forms, it doesn't show the name. It just says name "" I'm using Ruby on Rails.
My html looks like;
Contact Us
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
My controller code looks like;
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message Sent."
else
redirect_to new_contact_path, notice: "Error Occured"
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
Probably your second text field is the problem, it's called :name twice...
change the second input and try it again
<%= f.text_field :name, class: 'form-control' %>
to
<%= f.text_field :email, class: 'form-control' %>

Type error on nested form

been banging my head on the table over this one. I have another nested form but this one is driving me crazy.
The error is:
TypeError in CompaniesController#create
no implicit conversion of Symbol into Integer
#company = Company.new(company_params)
The controller:
class CompaniesController < ApplicationController
layout 'welcome'
def new
#company = Company.new
end
def create
#company = Company.new(company_params)
if #company.save
flash[:notice] = "New company created successful."
redirect_to admin_accounts_path
else
flash.now[:alert] = "Creation failed, please try again"
render :new
end
end
private
def company_params
params.require(:company).permit(:name, :location, :users_attributes => [:email, :password])
end
end
On the new.html.erb:
<%= render partial: 'form', locals: { company: #company, users_attributes: :users_attributes } %>
This code looks exactly like another nested setup I have, but it works :p
I had read that sometimes changing the params from => to just having a semicolon works, but replacing the user_attributes => with a user_attributes: didn't change anything.
EDIT: form.html.erb
<%= form_for company, url: companies_path do |f| %>
<div class="col-12">
<div class="row">
<div class="col p-0 mr-3">
<div class="form-group">
Company <%= f.label :name %>
<%= f.text_field :name, :placeholder => 'Company name', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :location %>
<%= f.text_field :location, :placeholder => 'Location', class: 'form-control' %>
</div>
</div>
<div class="col p-0">
<%= f.fields_for users_attributes do |user_f| %>
<div class="form-group">
<%= user_f.label :email %>
<%= user_f.text_field :email, :placeholder => 'Your Email Address', class: 'form-control' %>
</div>
<div class="form-group">
<%= user_f.label :password %>
<%= user_f.password_field :password, :placeholder => 'Password', class: 'form-control' %>
</div>
<% end %>
</div>
<div class="col-12 p-0">
<%= f.submit "Sign-Up", :class => 'btn btn-primary btn-block btn-lg' %>
</div>
</div>
</div>
<% end %>
Use users instead of users_attributes on the form.
Don't forget to define accepts_nested_attributes_for :users in model Company
## new.html.erb
<%= render partial: 'form', locals: { company: #company, users_attributes: :users } %>
I wonder why you didn't put :users into the form directly

After hitting send button the form submits but how do I redirect to new page in rails?

Here's my form code. After hitting send I'd like to see a confirmation message or redirect to a different page.
<%= flash[:notice]
<%= form_with(url: "/static_pages/thank_you") do |form| %>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control", placeholder: "Enter Name" %>
</div>
<div class="form-group">
<%= form.label :email, "Email Address" %>
<%= form.text_field :email, class: "form-control", placeholder: "Enter Email" %>
</div>
<div class="form-group">
<%= form.label :message %>
<%= form.text_area :message, class: "form-control", placeholder: "Enter Message" %>
</div>
</div>
</div>
<%= form.submit "Send" %>
<% end %>
Here is my controller code or at least the start of it. I'm suspecting I'm missing something on the page below.
class StaticPagesController < ApplicationController
def index
##products = Product.all
end
def landing_page
#products = Product.limit(4)
end
def contact
end
def about
end
def thank_you
#name = params[:name]
#email = params[:email]
#message = params[:message]
UserMailer.contact_form(#email, #name, #message).deliver_now
end
end
def thank_you
#name = params[:name]
#email = params[:email]
#message = params[:message]
UserMailer.contact_form(#email, #name, #message).deliver_now
flash[:notice] = "Mail sent successfully!"
redirect_to root_path
end

Rails 4 - ActiveModel::ForbiddenAttributesError

I have read other issues about it, but still I can't point what is causing the error. I have defined the strong parameters of Rails 4, but it keeps showing the error:
ActiveModel::ForbiddenAttributesError in MessagesController#create
My view is this:
<%= form_for(#message) do |f| %>
<div class="form-group field">
<%= f.label :phrase %>
<br/>
<%= f.text_field :phrase, autofocus: true, class: 'form-control' %>
</div>
<div class="form-group field">
<%= f.label :date %>
<br/>
<%= f.date_field :date, class: 'form-control' %>
</div>
<div class="actions text-center">
<%= f.submit "Submit", class: 'btn btn-default' %>
</div>
<% end %>
My controller:
class MessagesController < ApplicationController
def today
#dates = Message.all()
end
def history
#messages = Message.history_checker
end
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "OK"
redirect_to root_path
else
render :action => 'new'
end
end
private
def message_params
params.require(:message).permit(:phrase,:date)
end
end
The error points to line 15 of controller #message = Message.new(params[:message]). Any ideas?
You just need to use message_params instead of params[:message]:
#message = Message.new(message_params)

My forum is not updating the table?

I am trying to update my table by using the fourm :-
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for Message.new do |f| %>
<%= f.label :To %>
<%= f.email_field :to, class: 'form-control' %>
<%= f.label :Subject %>
<%= f.text_field :subject, class: 'form-control' %>
<%= f.label :Content %>
<%= f.text_area :content, class: 'form-control' %>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>
</div>
</div>
and in my controller I have done :-
def create
#message = Message.new(user_params)
redirect_to root_url
end
def user_params
params.require(:message).permit(:to, :subject, :content)
end
But when i check my Table, there is no update .
Where am i going wrong ?
My table have 2 more extra attributes , but i am not filling them up with the forum .
.new just builds the new object, you need to actually save it to persist it to the database.
def create
#message = Message.new(user_params)
#message.save!
redirect_to root_url
end

Resources