Rails submit form with get parameters from url - ruby-on-rails

I have form for signup user and when this url have get parameter like
http://localhost:3000/signup?list_id=10
need transfer this parameters to action for create user
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', user: #user %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit yield(:button_text), class: "btn btn-primary" %>
in this action user create dont see this parameter
def create
debugger
#user = User.new(user_params)
if #user.save
log_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render "new"
end
end
(byebug) params<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"I5+lc0T2fqy2Ie56rMnkR6Eff60nJmiuaTob7xAqknB6YgHZpmEByyRpCanpnNqyO9H/wMWbm7IumdXRyUABcA==", "user"=>{"name"=>"Ivan", "email"=>"ss#ss.com", "password"=>"222", "password_confirmation"=>"222"}, "commit"=>"Create my account", "controller"=>"users", "action"=>"create"} permitted: false>

It's available in your params hash so you can add it to your form as a hidden field so it will be submitted with the other params. Then make sure to whitelist that parameter in your controller.
First, add the virtual attribute to your User model if :list_id isn't already a persisted attribute for Users. This is done by adding the following line in your user.rb model file:
attr_accessor :list_id
Then add the following inside your form view:
<%= f.hidden_field :list_id, :value => params[:list_id] %>
Then in your controller where you whitelist your params you would add :list_id as a secure parameter. You didn't post your strong_params method but should be like:
def user_params
params.require(:user).permit(:list_id, :name, :email, :password, :password_confirmation)
end

Related

How get params from link ruby on rails?

I have column inviter in my User model. I want get params from the link and save it in cookies. For example, from this link:
domain.com/?ref=5
I want save to cookies number ?ref=**5**
And when a user goes to registration, the user will not need to write in the number of the referral in the form, because it is already in cookies.
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name,'Your name'%>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email,'Email' %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :invite, 'Number who invited you' %>
<%= f.text_field :invite, class: 'form-control' %>
<%= f.label :password, 'password' %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation ,'password confirmation' %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<br>
<br>
<div class="textc">
<%= f.submit yield(:button_text), class: "formbut" %>
</div>
<% end %>
The invite number will use ?ref=**5** and register the new user.
Add something like this to your application_controller.rb:
before_action :store_ref
private
def store_ref
cookies[:ref] = params[:ref] if params[:ref].present?
end
And something like this to your user create action:
def create
#user = User.new(user_params)
#user.ref = cookies[:ref]
if #user.save
# ...
A better approach would be to use a hidden input and populate it with the value from params[:ref].
class UsersController < ApplicationController
def new
#user = User.new do |u|
if params[:ref].present?
u.inviter = params[:ref]
end
end
end
# ...
end
<%= form_for(#user) do |f| %>
# ...
<% if f.object.inviter.present? %>
<%= f.hidden_field :inviter %>
<% end %>
<% end %>
It requires less workarounds.

Form data not getting saved to database

I'm new to RoR and I'm trying to create a sign-up form. When I click on 'Create my account', data does not get saved to the database. I have added resources: users to my routes file. Is there something else I'm missing?
This is my view (signup.html.erb)
Sign Up
<%= form_for #user, :url => { :action => "create" } do |f| %>
<%= f.label :emp_id,"Employee ID" %><br>
<%= f.text_field :emp_id, class: 'form-control' %><br><br>
<%= f.label :password, "Password" %><br>
<%= f.password_field :password, class: 'form-control' %><br><br>
<%= f.label :password_confirmation, "Password Confirmation" %><br>
<%= f.password_field :password_confirmation, class: 'form-control' %><br><br>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
This is my controller
class UsersController < ApplicationController
def new
#user = User.new
end
def signup
#user = User.new
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(user_params)
if #user.save
render 'login'
else
render 'signup'
end
end
def user_params
params.require(:user).permit(:emp_id, :password)
end
end
You need to permit :password_confirmation
def user_params
params.require(:user).permit(:emp_id, :password, :password_confirmation)
end
And make sure there are no other validations like email presence or that will cause validations to fail

modal form not creating user

I have created forms in rails with no problems before, but this is my first time doing it in a modal and I am not sure if that effects things. I don't think it does, but you never know. The problem is that new users are not being saved, and I am getting no useful errors as to why. Here is my form, in the modal:
<div class="modal-body">
<%= form_for #user do |f| %>
<%= f.label :first_name %><br>
<%= f.text_field :first_name, class: 'form-control', :required => true %><br>
<%= f.label :last_name %><br>
<%= f.text_field :last_name, class: 'form-control', :required => true %><br>
<%= f.label :user_name %><br>
<%= f.text_field :user_name, class: 'form-control', :required => true %><br>
<%= f.label :password %><br>
<%= f.password_field :password_digest, class: 'form-control', :required => true%><br>
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation, class: 'form-control', :required => true%><br>
</div> <!-- modal body -->
<div class="modal-footer">
<%= f.submit "Sign Up"%><br>
</div>
<% end %>
and here is what is in my users controller:
def create
#user = User.create(user_params)
if #user.save
redirect_to root_path
else
redirect_to({:controller => 'application', :action => 'home'}, :notice => 'Account not created! Something is fishy.')
end
end
def user_params
params.require(:user).permit(:first_name, :last_name, :user_name, :password)
end
More than likely, I am missing something. My question is two part: A)what is wrong and B)how can I stop the modal window from closing and have it display the appropriate error to the user giving them a chance to fix it?
Try:
#user = User.new(user_params)
instead of:
#user = User.create(user_params)
Because, you are trying to save the user after this call.
When you use create, it calls create followed by save.
If you use: User.new(user_params), it will just load the user object with the specified user_params but will not save it. In the next line, when you call: #user.save then the user object will be saved if valid. Otherwise, not.
Also, you can try using: #user.save! instead of #user.save. Because, when you try to save an object using save! method, it will raise an exception if it can't save the object properly. That way, you will know if something went wrong.
Observe the elements/tags "" should inside form_for becuase closing tag is inside of it.
<%= form_for #user do |f| %>
<div class="modal-body">
......your stuff fields here
</div> <!-- modal body -->
<div class="modal-footer">
<%= f.submit "Sign Up"%><br>
</div>
<% end %>

Rails map one field in form to two fields in model

I've seen a lot of posts about how to map 2 form fields to one model field, but what about mapping 1 form field to provide answers for two (or more) model fields? I have a form for users which gives a field, last_name, for a user. But I want the default password for that user to also be last_name (and I have password_confirmation set up, so that also needs to be last_name). How would I do this?
Form:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, :password, :password_confirmation,
class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.hidden_field :access_level, :value => "Chair" %>
<%= f.label :Phone %>
<%= f.text_field :phone_number, :id => "phone", class: 'form-control' %>
<p> </p>
<div class="col-md-12">
<%= f.submit "Add Chair", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
user_controller.rb
def create
#user = User.new(user_params)
if #user.save
log_in #user
current_user
flash[:success] = "Welcome to the Penn Model Congress!"
redirect_to after_sign_in_path
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password,
:password_confirmation, :access_level,
:phone_number)
end
end
I would add a before_validation callback to the User model like this:
# in app/models/user.rb
before_validation :set_default_password
private
def set_default_password
self.password ||= last_name
self.password_confirmation ||= last_name
end
If this will be your default and you don't want the user to be able to override it on creation, you could set it on the controller:
View:
<%= f.text_field :last_name, class: 'form-control' %>
Controller:
def create
#user = User.new(user_params)
#user.password = #user.last_name
#user.password_confirmation = #user.last_name
if #user.save
log_in #user
current_user
flash[:success] = "Welcome to the Penn Model Congress!"
redirect_to after_sign_in_path
else
render 'new'
end
end

Rails form_for password_field return type

I have a simple signup form:
<h1>Signup as a new user</h1>
<%= form_for(#user) do |f| %>
<p>
<%= f.label :username %>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.submit "Save" %>
</p>
<% end %>
That redirects to the create method of the UsersController:
def create
password = params[:password].crypt("$1$}")
#user = User.new(:username => params[:username], :password => password)
#user.save
flash[:message] = "User #{User.username} created!"
redirect_to user_path(#user)
end
But this throws an error undefined method crypt' for nil:NilClass. Why would the password be Nil? I checked the HTML params and got this:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aAcaURTLKfwmXULEUzLX36tZAKER/kMxKKOROOXgoU8=", "user"=>{"username"=>"Chris", "password"=>"[FILTERED]"}, "commit"=>"Save"}
Does "[FILTERED}" mean it isn't a string? How can I access it from params?
Its not nil, it just exists inside of :user, so try
params[:user][:password].crypt("$1$")
In fact all of your params work here is incorrect, all the fields are scoped by :user, this is one of the things that form_for does for you automatically.
So to get the username, you'd want to say params[:user][:username].

Resources