How do I edit a user's info in rails app? - ruby-on-rails

I have built a small and simple rails app that I deployed on heroku where you have to sign up as a user by creating a profile, after which you can enter an address or zipcode to find the nearest gyms in your vicinity, which triggers the google places/geocoder api and brings you some results.
My issue is that in trying to CRUD my app, I can't get the U, basically the update to work. I'll paste my code below and as you'll see I put a binding.pry and tested it directly in the rails console to see if my code is right and it works, I'm able to edit a user's info which is then updated when I go into psql and look in my db. But trying to use the browser to make the update does not work.
This is in my users_controller.rb file:
def update
#user = User.find(params[:id])
# binding.pry
#user.update(name: params[:name], password: params[:password], email: params[:email], zipcode: params[:zipcode])
redirect_to '/users'
end
This is the template code:
<h4 id="edit"> Edit your profile below: </h4>
<form action="/users/<%= #user.id%>" method="POST">
<div id="label">
<label>
<input id="one" name="name" placeholder="Edit name"/>
</label>
<label>
<input id="two" name="email" placeholder="Edit email"/>
</label>
<br>
<br>
<label>
<input id="three" name="password" placeholder="Edit password"/>
</label>
<label>
<input id="four" name="zipcode" placeholder="Edit zipcode"/>
</label>
</div>
<input name="_method" value="PUT" type="hidden" />
<input name="authenticity_token" value="<%= form_authenticity_token()%>" type="hidden">
<div id="update">
<br>
<button id="update">Update your profile</button>
</div>
</form>
The routes work and I'm taken to a confirmation page that I set up, but when I try to login with my updated info, the old info is still persistent. No errors are shown either that stop me from doing anything in the app. Any help is greatly appreciated, thank you!
I see these errors in my terminal:
, #messages={:email=>["Not a valid email format"]}>
, #messages={:email=>["Not a valid email format"]}>

Solved I used the advice of RichadAE and looked into the logs of my terminal to see the error messages and I saw that one of the errors was that when I was updating my user info, I was just updating the name or the zip for testing purposes which is fine, but because I am using the gem 'validates_email_format_of' the email field cannot be left blank even when updating otherwise it breaks my code, I tried to edit the name and filled in the email input field to meet the requirement and it worked!!! Thank you for your help!!

Related

Unable to store cookies in Rails

LoginController:
class LoginController < ApplicationController
def new
cookies[:id]=rand(1000)
render "index"
end
end
index.html.erb:
<body>
<center>
<h1> Login </h1>
<form action='login/auth' method='POST'>
Name: <input type="text" name="name" widht=20><br>
Password: <input type="password" name="pass" widht=20><br>
<input type="submit" value="submit">
</form>
</center>
<br><br>
</body>
My requirement is, if login page is called, the response page should send with a cookie. So, I create a cookie with a random number in
new action. But the page "index.html" didn't have cookie in browser. But it is properly worked if "new" action call its default view file
"new.html.erb". Why the cookie only works in default view and not in some other view file. How to solve this problem ?
FYI, you have two typos in the form. "width" is typed as "widht."
<form action='login/auth' method='POST'>
Name: <input type="text" name="name" widht=20><br>
Password: <input type="password" name="pass" widht=20><br>
<input type="submit" value="submit">
</form>
This won't solve the problem, but it will remove one more error once the problem is solved.
Related to the original problem, I wonder if it could be that when you want to pass a variable to a non-default view, you may need an instance variable (but since cookies aren't expressed as instance variables, I'm not sure where to go with that)

non-customized devise reset password button is doing absolutely nothing

I have devise login and logout working, been using it for a while now.
I have not copied devise's password controller or password views to my application, just using the out of the box form for password/new. After entering the email of a valid user, when I click the "send me password reset instructions" button, nothing happens. I checked my logs and there is no activity post-click. I've read through devise documentation but it doesn't seem like there is any special setup steps to get the reset password functionality working. What could be wrong?
For more context, I have followed devise's instructions for requiring admin to activate account before sign in. I have pulled the registrations and sessions controllers into my app for minimal customization, though that doesn't seem like it should matter. I'm using rails 4.2.0.rc2 and devise 3.4.1
Here's the rendered html for the form:
<h2>Forgot your password?</h2>
<form class="new_user" id="new_user" action="/users/password" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="AE+YY8afzRavKa1ziDFZ5Z6FqFYQF1vI+zvc701JGqfg5k+ztvMIKHDX4Wdfr+KOp9yEcRBTsk4jfonpzUeP5w==" />
<div class="field">
<label for="user_email">Email</label><br />
<input autofocus="autofocus" type="email" value="" name="user[email]" id="user_email" />
</div>
<div class="actions">
<input type="submit" name="commit" value="Send me reset password instructions" />
</div>
</form>
your javascript is preventing the form from being submitted unless a terms-of-service field is checked.
from your application.js file:
if $('#user_terms').is(':checked')
else
e.preventDefault()
$('#terms-of-service-error').modal()
return

Force capitalization in iOS web page?

This should be easy to fix, but I can't find out why it's not working.
I have a web "app" that runs in Safari, and it's made for users to add to the home screen. There's a web form where the user enters a first and last name. Here's the simple code for that form.
<fieldset>
<div class="row">
<label>First Name</label>
<input type="text" name="first_name" id="pFirstName" value="" autocapitalize="on" autocorrect="off" placeholder="John" />
</div>
<div class="row">
<label>Last Name</label>
<input type="text" name="last_name" id="pLastName" value="" autocorrect="off" autocapitalize="on" placeholder="Doe" />
</div>
</fieldset>
Very simple. The problem is that the Last Name form is not capitalized when the user switches to it. I ran it directly in mobile Safari, and it worked fine. When I added to the home screen, I had the same issue...
Is this just a bug in iOS/webkit (because it used to work...)? Not sure if there's some sort of script to force the auto-capitalization? Thanks!
Edit: If I click on the field directly, it capitalizes. The problem is when I fill in text for first name, and click the "Next" arrow on the top of the keyboard.
Try using autocapitalize="sentences"

Create a search page in ActiveAdmin

I'm using ActiveAdmin to deploy my project. And I had some problem when I was developing.
I had some database table, e.g: "Worker", "Product", "Task". I wanted to create a page to search on those table with many situation.
I created a simple page:
ActiveAdmin.register_page "my page" do
content do
panel "Search details" do
panel "By Name" do
render :partial => "form"
end
end
end
end
And this is _form.html.erb
<form action="" method="post">
<input type="text" value="" name="taskid">Task ID</input>
<input type="text" value="" name="productid">Product ID</input>
<input type="text" value="" name="workerid">Worker ID</input>
<input type="submit" value="Submit"/>
</form>
But I don't know how can I call a controller from a form? (which controller was defined)
And How can I render or show the result to the content area of "my_page" in Activeadmin from a controller?
Anyone may help me? plz!
Design the form such that it uses the existing active admin filters and displays the results accordingly. You may want to look at the HTML of your filter forms (using firebug), in order to get the required params, and the submit action. Here is the partial which I use to search my User model (constructed from user filters):
<div class="panel_contents">
<form method="get" id="q_search" class="filter_form" action="/admin/users" accept-charset="UTF-8">
<div class="filter_form_field filter_string">
<label for="q_email" class=" label">Search User Email</label><br/>
<input type="text" name="q[email_contains]" id="q_email" />
<input type="submit" value="Go" name="commit" id="q_submit" />
</div>
</form>
</div>
and displays the result in the existing format. You don't need to design new views for that.

Basic form action not working on heroku (but works locally)

EDIT: I feel so stupid, I wasn't merging my branches properly with git. Like I said, I am quite new to programming and am prone to these types of errors. I appreciate the help, though.
I am on ch. 7 of the Rails by Example book, where I created a basic "Sign up" form. When I submit the form on my localhost, it works properly by directing me to either
LOCALHOST/new (back to sign up page, registration info error) or LOCALHOST/show (user profile, success).
When I push this same app to heroku and hit the submit button, it always leads me to this url:
appname.heroku.com/form_action.asp?fname=&email=&password=&password= which tells me
"The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved."
Instead of appname.heroku.com/new or /show.
I am so confused, I am a beginner programmer and it boggles my mind why things work so perfectly on the localhost but not on the production server. Please help!
My code is on github:
https://github.com/Chamza/Sample-App2
Are you running the 'cedar' stack on Heroku? I believe the url should be appname.herokuapp.com -- not appname.heroku.com. Since your code in github is based using rails 3.2.1, you should be using the cedar stack.
Here's some information on the cedar stack from heroku: https://devcenter.heroku.com/articles/cedar
Also, can you run heroku logs --tail --app my-app-name and then post what happens when you click the link? That's known as 'tailing the log' and it will give you real-time output of what's happening.
EDIT***
I looked at your code and I can see that at least one problem is the form you're using has:
<% provide(:title, 'Sign up') %>
<div style = "width: 200px; margin: 0 auto;">
<form action="form_action.asp" method="get">
<h1>Sign up</h1>
<p align="left">Name:</p>
<input type="text" name="fname" />
<p>Email:</p>
<input type="text" name="email" />
<p>Password:</p>
<input type="password" name="password" />
<p>Password Confirmation:</p>
<input type="password" name="password" />
<br/>
<input type="submit" value="Submit" />
</div>
</form>
There are a few errors in the form. First, the action is to form_action.asp. That looks like it came from an old project. Second, the end </div> and </form> tags are switched -- the form tag should be before the div tag.
<% provide(:title, 'Sign up') %>
<div style = "width: 200px; margin: 0 auto;">
<form action="form_action.asp" method="get">
<h1>Sign up</h1>
<p align="left">Name:</p>
<input type="text" name="fname" />
<p>Email:</p>
<input type="text" name="email" />
<p>Password:</p>
<input type="password" name="password" />
<p>Password Confirmation:</p>
<input type="password" name="password" />
<br/>
<input type="submit" value="Submit" />
</div>
</form>
It's likely that your local machine has different code on it than what you have deployed on Heroku. I'd check carefully -- do a heroku releases --app my-app and it will tell you what the latest git commit is on the server.

Resources