Ruby on Rails param is missing or the value is empty without using a model - ruby-on-rails

In my contact.html, I have this code
<div class="form">
<form name="email-form" method="POST">
<label class="field-label" for="name">Name:</label>
<input class="w-input text-field-2" id="name" type="text" name="name" data-name="Name" required="required">
<label class="field-label" for="Email">Email:</label>
<input class="w-input text-field-2" id="email" type="email" name="email" data-name="Email" required="required">
<label class="field-label" for="Subject">Subject:</label>
<input class="w-input text-field-2" id="subject" type="text" name="subject" data-name="Subject">
<label class="field-label" for="Content">Text Message:</label>
<textarea class="w-input text-field-2 area" id="content" name="content" data-name="Text Area" required="required"></textarea>
<div>
<input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
</div>
</form>
</div>
This is under my controller
def contact
end
def send_mail
MessageMailer.new_message(contact_params).deliver
redirect_to contact_path, notice: 'Your messages has been sent.'
end
private
def contact_params
params.require(:contact).permit(:name, :email, :subject, :content)
end
under mailers/message_mailer.rb
class MessageMailer < ActionMailer::Base
default from: "sys.questdentalusa#gmail.com"
default to: "questdentalusa#gmail.com"
def new_message(contact)
#contact = contact
mail subject: #contact.subject
end
end
and under my new_message.text.erb is this code
Name: <%= #contact.name %>
Email: <%= #contact.email %>
Message: <%= #contact.content %>
I am to send an email consisting user's name, email and message which is inputed and NOT saved in the database. When I pass the four parameters like this
def send_mail
MessageMailer.new_message(:name, :email, :subject, :content).deliver
redirect_to contact_path, notice: 'Your messages has been sent.'
end
it worked just fine but i was told to use only one parameter, seems like group the four parameters: name, email, subject, content as one (contact)
When I typed the info and hit the submit button, I get this error message
param is missing or the value is empty: contact
I presume that what caused this error is because my def contact is empty. So I added contact.new and #contact=contact.new and MessageMailer.new but this error occurs NoMethodError
How can I possibly fix this? What should I write under my def contact ?

Your controller code is correct. You don't need Contact.new or something. The problem is with your <form>. What ends up in params depends on your form and in your form you don't have contact.
Instead of:
<input type="text" name="subject">
You have to do something like this:
<input type="text" name="contact[subject]">
And that for all the fields on your contact form.
Another option would be to use Rails' form helpers.

contact[] is missing in your form, see below correct one:
<div class="form">
<form name="email-form" method="POST">
<label class="field-label" for="name">Name:</label>
<input class="w-input text-field-2" id="name" type="text" name="contact[name]" data-name="Name" required="required">
<label class="field-label" for="Email">Email:</label>
<input class="w-input text-field-2" id="email" type="email" name="contact[email]" data-name="Email" required="required">
<label class="field-label" for="Subject">Subject:</label>
<input class="w-input text-field-2" id="subject" type="text" name="contact[subject]" data-name="Subject">
<label class="field-label" for="Content">Text Message:</label>
<textarea class="w-input text-field-2 area" id="content" name="contact[content]" data-name="Text Area" required="required"></textarea>
<div>
<input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
</div>
</form>
</div>

Related

How to add validation to contact form?

I am trying to add a contact form for users to fill and it will automatically email to the main account. I am unsure as to how I can add validation to the form so users are not able to click "Send Email" without filling in the fields.
I have currently tried adding Website: <input type="url" name="website" required>
app/views/contact_mailer/contact_email.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Contact Us Form</h1>
<p>
From: <%= #params['name'] %>
</p>
<p>
Email: <%= #params['email'] %>
</p>
<p>
Message: <%= #params['message'] %>
</p>
</body>
</html>
Contact form:
<h1>Any enquiries:</h1>
<center><form name="htmlform" method="get" action="/pages/send_form">
<table width="700px">
<form id="contact_form" action="#" method="POST" enctype="multipart/form-data">
<div class="row">
<label for="name">Your Name:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="email">Your Email:</label><br />
<input id="email" class="input" name="email" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="message">Your Message:</label><br />
<textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
</div>
<input id="submit_button" type="submit" value="Send email" />
</form>
Currently, the email still sends to the account without any validation such as "You must complete this field"
Create a model for your contact message. Something like:
class ContactEmail
attr_accessor :name, :email, message
end
Then include ActiveModel::Model to get ActiveModel's validation and tell the model which fields to validate, like:
class ContactEmail
include ActiveModel::Model
attr_accessor :name, :email, message
validates :name, :email, message, presence: true
end
Finally, in your controller action, create a new ContactEmail with the form data and call the predicate method valid?. And use flash to add an error message.
new_mail = ContactEmail.new(params['name'], params['email'], params['message'])
if new_mail.valid?
...
else
flash.now[:error] = new_mail.errors.full_messages
end
Could you use the HTML form attributes? There is a required attribute so the user can't leave the field empty.
e.g.,
<input required id="email" class="input" name="email" type="email" value="" size="30" />
you can also use the type = "email" and it will ensure some of the proper formatting.
(https://www.w3schools.com/html/html_form_attributes.asp)
Looking at the code of your form i see several problems:
you have 2 <form> openings
i dont see any file inputs so you don't need the enctype="multipart/form-data"
here's the code revised (i've removed the first form element, i've adjusted the action of the second form, i've added required attributes to the inputs and textarea and i've updated the email input's type to "email" to make sure only valid emails are entered). Adding required should force modern browsers to prevent form submission if the inputs are not filled in properly.
<h1>Any enquiries:</h1>
<center>
<table width="700px">
<form id="contact_form" action="/pages/send_form" method="POST">
<div class="row">
<label for="name">Your Name:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" required /><br />
</div>
<div class="row">
<label for="email">Your Email:</label><br />
<input id="email" class="input" name="email" type="email" value="" size="30" required /><br />
</div>
<div class="row">
<label for="message">Your Message:</label><br />
<textarea id="message" class="input" name="message" rows="7" cols="30" required></textarea><br />
</div>
<input id="submit_button" type="submit" value="Send email" />
</form>

rails adding to database

I am attempting to add product information to a rails sqlite database through a form. I can get users to be added but not the products for the store. This is the form.
<form action='/products/create' method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<label>Name:
<input type="text" name="product">
</label>
<label>Amount:
<input type="number" name="amount">
</label>
<center><input type="submit" value="Sell"></center>
</form>
It does not throw an error but it does not save to db either.
Products_controller is
def create
#products = Product.new(name: params[:name], amount: params[:amount])
if #products.valid?
#products.save
flash[:message] = "New product added"
redirect_to "/users"
else
flash[:errors] = #products.errors.full_messages
redirect_to "/products"
end
I figured it out. The problem was this line.
<input type="text" name="name">
In my database product is defined as name and amount. Product does not have a product field.

I used pry to see what happens when I hit the login button and the information isn't saving to the database

I just learned about pry for debugging in rails, I'm working on a ecommerce site , on the main sign in page, when the login button is clicked, the email, username and the password field of the login form is suppose to save to the database. Instead of getting Example A, I get Example B but I'm not sure what {"controller"=> "application", "action" => "login"} means. Can someone help me please.
Example:A
correct result
Example:B
[wrong result pasted below][1]
23: def login
24: #username = params[:username]
25: #email = params[:email]
26: #password = params[:password]
=> 27: binding.pry
28: if #email && #password
29: session[:signed_in] = true
30: session[:username] = params[:username]
31: redirect_to '/profile'
32: end
33: end
[1] pry(#<ApplicationController>)> #username
=> nil
[2] pry(#<ApplicationController>)> params
=> {"controller"=>"application", "action"=>"login"}
[3] pry(#<ApplicationController>)>
My sign in form:
<form action='/login' class='validate-form' method='post'>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Email Address
<em>*</em>
</label>
<input type="email">
</p>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Username
<em>*</em>
</label>
<br>
<input type="email">
</p>
<p class="checkout-coupon top-down log a-an">
<label class="l-contact">
password
<em>*</em>
</label>
<input type="password">
</p>
<div class="forgot-password1">
<label class="inline2">
<input type="checkbox" name="rememberme7">
Remember me! <em>*</em>
</label>
<a class="forgot-password" href="#">Forgot Your password?</a>
</div>
<p class="login-submit5">
<input class="button-primary" type="submit" value="login">
</p>
</form>
My routes.rb:
Rails.application.routes.draw do
get '/profile', to:'application#profile'
get '/logout', to: 'application#logout'
post '/login', to: 'application#login'
end
In answer to your specific question
I'm not sure what {"controller"=> "application", "action" => "login"} means
This is a Ruby Hash with two key value pairs that is referenced by your link within the form. The value of the key controller refers to the specific controller that you are linking to with your form. In this case your ApplicationController as defined by the value application. The value of the key action refers to the specific method within that controller that you are calling.
In your form you have <input type="email">, but no name attributes. Name attributes are used by the server to identify the fields in the form.
link check this for more information.
Your params attributes are just {"controller"=>"application", "action"=>"login"} because thats where the form is being submitted, To your application controller and login action, your login action is defined in your form <form action='/login' class='validate-form' method='post'> here /login, thats your action.
In this case login. To get example A, your form should be like this
<input type="email" name='email'>
<input type="text" name='username'>
<input type="password" name='password'>
If you post your form I can try to provide an answer to your other problems.
Change the form to this and try again,
Try adding names to the input fields, and then use the pry and insect the params from it.
Also did you use the strong parameters for the fields?
<form action='/login' class='validate-form' method='post'>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Email Address
<em>*</em>
</label>
<input type="email" name="email">
</p>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Username
<em>*</em>
</label>
<br>
<input type="email" name="username">
</p>
<p class="checkout-coupon top-down log a-an">
<label class="l-contact">
password
<em>*</em>
</label>
<input type="password" name="username">
</p>
<div class="forgot-password1">
<label class="inline2">
<input type="checkbox" name="rememberme7">
Remember me! <em>*</em>
</label>
<a class="forgot-password" href="#">Forgot Your password?</a>
</div>
<p class="login-submit5">
<input class="button-primary" type="submit" value="login">
</p>
</form>

How to post html form data to controller using ruby on rails 3.1

I am using ruby on rails 3.1. And am trying to post html form data to controller for saving the record in database. But I am getting routing error like this 'No route matches [POST] first/save' .But when I tried to run this link in address bar like '127.0.0.1:3000/first/save' it is working fine. Can any one please tell me where am doing wrong.
My routes are like:
Rails.application.routes.draw do
root 'first#hello'
get 'first/save'
end
And my html form is like:
<form accept-charset="UTF-8" method='post' action='/first/save'>
<label for='S.No'>S.No</label>
<input type="text" name="s_no" placeholder='Enter s. no.'>
<label for='name'>Name</label>
<input type="text" name='name' placeholder='Enter your name'> <br>
<label for='seller_id'>Seller ID</label>
<input type="text" name='seller_id' placeholder='Enter your seller ID'> <br>
<label for='email'>Email</label>
<input type="email" name='email' placeholder='Enter your email'> <br>
<label for='img_url'>Image</label>
<input type='text' name='img_url' placeholder='Enter your image url'> <br>
<input type="submit" name='save' value='Save'>
</form>
And here is my controller:
class FirstController < ApplicationController
def save
#name = params[:name]
#email = params[:email]
#seller_id = params[:seller_id]
#img_url = params[:img_url]
#s_no = params[:s_no]
end
end
If you want to do POST requests, instead of
get 'first/save'
you should have
post 'first/save'

Can't select checkbox using id in Capybara

There is the following RSpec code:
describe 'with valid information' do
it 'should create a new restaurant' do
visit new_restaurant_path
fill_in I18n.translate(:name), with: "Some restaurant"
fill_in I18n.translate(:address), with: "Some address of the restaurant"
fill_in I18n.translate(:average_check), with: 100
check('place_restaurant_food_types_1')
expect { click_button :submit }.to change(Restaurant, :count).by(1)
end
end
But I always get error "cannot check field, no checkbox with id, name, or label 'place_restaurant_food_types_1' found". I tried to replace id for name, but it was still the same. I'm absolutely sure that there is the item with needed id! (I copied it from the page source). How can I fix it?
HTML:
<form accept-charset="UTF-8" action="/restaurants" class="new_place_restaurant" id="new_place_restaurant" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="7yQCirO7MLO6e+Nj46eCSNUjQWd67MJVDIHmUV6/5Y0=" /></div>
<div class="row">
<label for="place_restaurant_name">Название</label>
<input id="place_restaurant_name" name="place_restaurant[name]" size="30" type="text" />
</div>
<div class="row">
<label for="place_restaurant_address">Адрес</label>
<input id="place_restaurant_address" name="place_restaurant[address]" size="30" type="text" />
</div>
<div class="row">
<label for="place_restaurant_average_check">Средний чек</label>
<input id="place_restaurant_average_check" name="place_restaurant[average_check]" size="30" type="text" />
</div>
<div class="row">
<input id="place_restaurant_food_types_1" name="place_restaurant[food_types][1]" type="checkbox" value="1" />
<label for="place_restaurant_food_types_1">Восточная кухня</label>
</div>
<div class="row">
<input id="place_restaurant_food_types_2" name="place_restaurant[food_types][2]" type="checkbox" value="1" />
<label for="place_restaurant_food_types_2">Национальная кухня</label>
</div>
<div class="row">
<input id="place_restaurant_food_types_3" name="place_restaurant[food_types][3]" type="checkbox" value="1" />
<label for="place_restaurant_food_types_3">Японская кухня</label>
</div>
<div class="row">
<input id="place_restaurant_food_types_4" name="place_restaurant[food_types][4]" type="checkbox" value="1" />
<label for="place_restaurant_food_types_4">Китайская кухня</label>
</div>
<div class="row">
<input id="place_restaurant_food_types_5" name="place_restaurant[food_types][5]" type="checkbox" value="1" />
<label for="place_restaurant_food_types_5">Европейская кухня</label>
</div>
<div class="row">
<input id="place_restaurant_food_types_6" name="place_restaurant[food_types][6]" type="checkbox" value="1" />
<label for="place_restaurant_food_types_6">Кавказская кухня</label>
</div>
<div id="map_canvas"></div>
<input id="latitude" name="place_restaurant[latitude]" type="hidden" value="54.352271" />
<input id="longitude" name="place_restaurant[longitude]" type="hidden" value="48.52652" />
<div class="row">
<input id="submit" name="commit" type="submit" value="Сохранить" />
</div>
</form>
You could just use
check("place_restaurant_food_types_1")
as shown in the docs:
The check box can be found via name, id or label text.
Try By Xpath like this
find(:xpath, "//*[#id='place_restaurant_food_types_1']").click
You can use like this
find(:css,"input[id='place_restaurant_food_types_1']").set true

Resources