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.
Related
i am trying to display a message for whenever someone does not fill the form completely and press the submit button!
Example: when someone clicks the submit button without filling the Name and Email field in the form, it should display
2 Errors Prohibited from submitting the form!
. Name can't be blank
. Email can't be blank
but it is not being displayed whenever we submit the form with empty fields
can someone explain me the reason why it is not working?
Orders_controller.rb
class OrdersController < ApplicationController
# GET to /orders/new
def new
#order = Order.new
end
# POST to /orders
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save!
format.html{ redirect_to root_url, notice: "Order Succesfully Submitted!" }
else
format.html{ render :new, status: :unprocessable_entity }
end
end
end
private
def order_params
params.require(:order).permit(:first_name, :last_name, :phone_number, :email, :paper_size, :color, :paper_style, :quantity, :description, files: [] )
end
end
_form.html.erb (i already rendered the partial in new.html.erb by <%= render 'form', order: #order%>
<div class="container">
<h1 class="text-center">Order From Home!</h1>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<%= form_with(model: order) do |f| %>
<% if order.errors.any? %>
<div style="color: red">
<h3><%= pluralize(order.errors.count,"errors")%> Prohibited from submitting the form! <br/>
All Fields Are Required!</h3>
<ul>
<% order.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :first_name%>
<%= f.text_field :first_name, class:"form-control" %><br/>
<%= f.label :last_name %>
<%= f.text_field :last_name, class:"form-control" %><br/>
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class:"form-control" %><br/>
<%= f.label :email %>
<%= f.text_field :email, class:"form-control" %><br/>
<%= f.label :files %>
<%= f.file_field :files, multiple: true %><br/>
<%= f.label :paper_size %>
<%= f.select :paper_size, ['A4', 'B4'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :color %>
<%= f.select :color, ['Black & White', 'Color'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :paper_style %>
<%= f.select :paper_style, ['Black to Back', 'Side to Side'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :quantity %>
<%= f.number_field :quantity, class:'form-control' %><br/>
<%= f.label :description %>
<%= f.text_area :description, class:"form-control" %><br/>
<div class="btn-order">
<%= f.submit %>
</div>
<% end %>
</div>
</div>
</div>
#order.save! raises validation errors. You don't need bang method if you want to render such errors, just use #order.save instead
form_with sends XHR request. If you need to render something, you can disable Turbo for this form
<%= form_with(model: order, data: { turbo: false }) do |f| %>
In my users views new.html.erb
<% provide(:title, 'SignUp') %>
<%= form_with(model: #user, local: true) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<br>
<%= f.label :email, "Email" %>
<%= f.email_field :email %>
<br>
<%= f.label :password, "Password" %>
<%= f.password_field :password %>
<br>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<br>
<%= f.submit "Create Account", class: "btn btn-outline-success" %>
<% end %>
In my user controller users_controller.rb looks like:-
class UsersController < ApplicationController
def new
#user = User.new
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(user_params)
if #user.invalid?
puts "Hi"
flash[:success] = "Invalid Data"
render 'new'
else
flash[:success] = "Welcome to sample app"
#user.save
redirect_to #user
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
This is my errors file /views/shared/_errors.html.erb
<% if #user.errors.any? %>
<div class="alert alert-danger">
The form contains <%= pluralize(#user.errors.count, "error") %>
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li> <%= msg %> </li>
<% end %>
</ul>
<% end %>
render 'new' is not working in create action. I include the errors.html.erb file in new file so it can display the error, but it is not working.
Try form_for instead of form_with.
<%= form_for(#user) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<br>
<%= f.label :email, "Email" %>
<%= f.email_field :email %>
<br>
<%= f.label :password, "Password" %>
<%= f.password_field :password %>
<br>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<br>
<%= f.submit "Create Account", class: "btn btn-outline-success" %>
<% end %>
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
I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect
I have to Devise models, User and Vendor. I generated the views for Vendor and customized the signup form to suit my needs. I needed to tweak the registrations controller for Vendor, like so:
controllers/vendors/registrations_controller.rb
class Vendors::RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication
def create
super
end
protected
def sign_up(resource_name, resource)
true
end
def new
super
end
end
My signup view for Vendor looks like so:
views/vendors/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multiparet => :true} ) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :store_name %>
<%= f.text_field :store_name %></div>
<div><%= f.label :contact_name %>
<%= f.text_field :contact_name%></div>
<div><%= f.label :contact_phone %>
<%= f.text_field :contact_phone %></div>
<div><%= f.label :address %>
<%= f.text_field :address %></div>
<div><%= f.label :image %>
<%= f.file_field :image %></div>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "vendors/shared/links" %>
When I attempt to render the page, I get this error:
undefined method `errors' for nil:NilClass
How can I render devise error messages in this situation? Thanks in advance!
I would write this as a comment, but it will be easier to read as an answer:
Perhaps you could try replacing devise_error_messages! with a standard Rails error display:
<% if resource.errors.any? %>
<div class="error">
<strong><%= pluralize(resource.errors.count, "Error") %></strong>
<ul>
<% resource.errors.each do |attr,msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>