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

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>

Related

Calling method in rails

In my application I have an form to create new company. Here I have to enter company name and company url.Here is my code for the from.
<%= form_tag(controller: "/company", action: "add_startup_to_index", method: "post") do %>
<div class="modal-body">
<div class="form-group">
<label class="control-label">Company Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter the name of the company..." required />
</div>
<div class="form-group">
<label class="control-label">Company URL</label>
<input type="text" class="form-control" id="url" name="url" placeholder="e.g. http://www.company.com..." required />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-conf" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary btn-conf">Add Company</button>
</div>
<% end %>
This is my method for saving above data.
def add_startup_to_index
#url = api_version_root + 'startups/new'
response = RestClient.post #url,
{ startup: { friendly_name: params[:name],
url: params[:url]
}
}, api_token_hash
record = JSON.parse(response.body)
flash[:info] = 'Startup has been added and Crunchbase sync started.'
redirect_to('/startups/' + record['company_id']) && return
rescue RestClient::ExceptionWithResponse => err
handle_rest_error http_code: err.http_code
end
This is working fine and I can save the companies. Now I want to validate the URL. For that I have below method.
def valid_url?(url)
return false if url.include?("<script")
url_regexp = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
url =~ url_regexp ? true : false
end
Since I am new for rails I have no idea how to call that method within my form. I had tried nested form_tag. But it is now allowed.
I tried as below.
<%= form_tag(controller: "/company", action: "add_startup_to_index", method: "post") do %>
<div class="modal-body">
<div class="form-group">
<label class="control-label">Company Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter the name of the company..." required />
</div>
<%= form_tag(controller: "/company", action: "valid_url", method: "post") do %>
<div class="form-group">
<label class="control-label">Company URL</label>
<input type="text" class="form-control" id="url" name="url" placeholder="e.g. http://www.company.com..." required />
</div>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-conf" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary btn-conf">Add Company</button>
</div>
<% end %>
Can any one help me for this.
Lanka, if I understand you correctly, what you are looking for is client side form validation. Before HTML5 this was a tedious task involving JS. With HTML5, url validation is baked right in. Simply try to change the type of the input to url, i.e. change
<input type="text" id="url" name="url" ...>
to
<input type="url" id="url" name="url" ...>
Then, when a non-conforming url string is entered, the browser will not submit the form and automatically indicate the issue. This even works out of the box with a default url pattern. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/url for more information on custom patterns, placeholders and much more.

Adding a new Favorite: Creating a form with the current_user.id

I'm trying to create a form that will save the current_user.id. Something is funky in my controller. Any thoughts on obvious issues?
View (new.html.erb)
<!-- Label and input for user_id -->
<div class="form-group">
<label for="user_id" class="control-label">
User
</label>
<%= current_user.id %>
<input type="hidden" name="user_id" value="<%= current_user.id%>">
</div>
Controller (favorites_controller.rb)
def create
#favorite = Favorite.new
#favorite.dish_comment = params[:dish_comment]
#favorite.user_id = curent_user.id
d = params[:dish_id]
r = params[:restaurant_id]
problem code:
#favorite.user_id = curent_user.id
I thing you should put an # in front of your current_user to make it accessible in the ERB and in the controller.
Plus, once you save the current_user, you should user params[:user_id] to match the erb (input name is user_id).
Please look at the rails server output, it will tell you what parameters are submitted in the request.
Thanks #jackhaskeyboard. I spelled "current" wrong.
I'm now getting the following errors: Add Favorite Error Messages
1. "User has already been taken"
-A user should be able to post unlimited favorites, so I"m not sure why this is occurring.
"Dishing can't be blank"
-The purpose of this form is to select two variables (dish & restaurant), search the dishing join table to see if that combination exists. If not, create one, if so, grab the dishing_id, and use it to create a new favorite entry (user & dish/restaurant)
Here's my view code:
<div class="row">
<div class="col-md-12">
<form action="/create_favorite" method="post">
<!-- Hidden input for authenticity token to protect from forgery -->
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>">
<!-- Label and input for user_id -->
<div class="form-group">
<% current_user.id %>
<input type="hidden" name="user_id" value="<%= current_user.id%>">
</div>
<!-- Label and input for dishing_id -->
<div class="form-group">
<label for="dishing_id" class="control-label">
Dish
</label>
<%= select_tag(:dish_id, options_from_collection_for_select(Dish.all, 'id', 'dish_name')) %>
</div>
<!-- Label and input for restaurant_id -->
<div class="form-group">
<label for="restaurant_id" class="control-label">
Restaurant
</label>
<%= select_tag(:restaurant_id, options_from_collection_for_select(Restaurant.all, 'id', 'name') ) %>
</div>
<button class="btn btn-success">
Create Favorite
</button>
or
Cancel
</form>
</div>
</div>

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

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>

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'

Getting text from a textfield

I am trying to get username and password from the textfield. This is the HTML code:
<form class="navbar-form pull-left">
<input type="text" class="span2">
<input type="text" class="span2">
<button type="submit" class="btn">Submit</button>
</form>
How can I get this username & password in the LoginController to check for authentication?
First, you need to add a name property to your input fields. Then specify the action and method attributes for the form. The form would look like this then:
<form action="your_route_goes_here" method="post" class="navbar-form pull-left">
<input name="username" type="text" class="span2">
<input name="password" type="password" class="span2">
<button type="submit" class="btn">Submit</button>
</form>
In your LoginController, you can then access the username and password like so, respectively:
params[:username]
params[:password]

Resources