After hitting send button the form submits but how do I redirect to new page in rails? - ruby-on-rails

Here's my form code. After hitting send I'd like to see a confirmation message or redirect to a different page.
<%= flash[:notice]
<%= form_with(url: "/static_pages/thank_you") do |form| %>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control", placeholder: "Enter Name" %>
</div>
<div class="form-group">
<%= form.label :email, "Email Address" %>
<%= form.text_field :email, class: "form-control", placeholder: "Enter Email" %>
</div>
<div class="form-group">
<%= form.label :message %>
<%= form.text_area :message, class: "form-control", placeholder: "Enter Message" %>
</div>
</div>
</div>
<%= form.submit "Send" %>
<% end %>
Here is my controller code or at least the start of it. I'm suspecting I'm missing something on the page below.
class StaticPagesController < ApplicationController
def index
##products = Product.all
end
def landing_page
#products = Product.limit(4)
end
def contact
end
def about
end
def thank_you
#name = params[:name]
#email = params[:email]
#message = params[:message]
UserMailer.contact_form(#email, #name, #message).deliver_now
end
end

def thank_you
#name = params[:name]
#email = params[:email]
#message = params[:message]
UserMailer.contact_form(#email, #name, #message).deliver_now
flash[:notice] = "Mail sent successfully!"
redirect_to root_path
end

Related

Contact name not showing name in DB

I'm a rookie, and looking to create a contact form, but when I check my submitted forms, it doesn't show the name. It just says name "" I'm using Ruby on Rails.
My html looks like;
Contact Us
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
My controller code looks like;
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message Sent."
else
redirect_to new_contact_path, notice: "Error Occured"
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
Probably your second text field is the problem, it's called :name twice...
change the second input and try it again
<%= f.text_field :name, class: 'form-control' %>
to
<%= f.text_field :email, class: 'form-control' %>

THE SUBMIT BUTTON

Udemy: Bootcamp course: section: 8 Lecture: 122: SAVING TO THE DATABSE: In the "Contact Us" page I filled in the info, Name: test1, Email: test1#example.com, Comments: test1, as a test, pressed submit, but the button do not submit the info and gives no error message. Where am I going wrong? Any expert advice? Here is my code:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
flash[:success] = 'Message sent.'
redirect_to new_contact_path
else
flash[:danger] = 'Error occured, message has not been sent.'
redirect_to new_contact_path
end
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well"
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
Im not a rails expert but you can try <%= form_for(#contact, :method => :get) do |f| %> that should get it to work.
In your contact.rb model type the following
class Contact < ActiveRecord::Base
end

Rails 4 - ActiveModel::ForbiddenAttributesError

I have read other issues about it, but still I can't point what is causing the error. I have defined the strong parameters of Rails 4, but it keeps showing the error:
ActiveModel::ForbiddenAttributesError in MessagesController#create
My view is this:
<%= form_for(#message) do |f| %>
<div class="form-group field">
<%= f.label :phrase %>
<br/>
<%= f.text_field :phrase, autofocus: true, class: 'form-control' %>
</div>
<div class="form-group field">
<%= f.label :date %>
<br/>
<%= f.date_field :date, class: 'form-control' %>
</div>
<div class="actions text-center">
<%= f.submit "Submit", class: 'btn btn-default' %>
</div>
<% end %>
My controller:
class MessagesController < ApplicationController
def today
#dates = Message.all()
end
def history
#messages = Message.history_checker
end
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "OK"
redirect_to root_path
else
render :action => 'new'
end
end
private
def message_params
params.require(:message).permit(:phrase,:date)
end
end
The error points to line 15 of controller #message = Message.new(params[:message]). Any ideas?
You just need to use message_params instead of params[:message]:
#message = Message.new(message_params)

Rails - Right / Better way to to a belongs_to / has_many relationship

I am creating an application through which a user will be able to create an account. When they create an account, in the same form they will be able to create an organization that will then be tied to their user. Once that user has created their account (and an organization) other users will be able to create an account and use an "access code" to join that organization as well. Looking at the code may explain it better.
The reason i'm posting on SO is because i have a feeling there is a better / more efficient way to do it than what i am currently doing. I'm using nested_forms (maybe not correctly) and i don't think i'm doing the associations the right way because, for example, i haven't been able to get the edit form to fill out the organization fields.
I am using sorcery for the authentication as well.
users_controller.rb
def new
#user = User.new
end
def create
#user = User.new(user_params)
if params[:user][:organization][:name].blank?
flash.now[:error] = "You must specify an organization name."
render :new
else
if params[:user][:organization][:access_code].blank?
# create new organization
#access_code = "#{SecureRandom.urlsafe_base64(16)}#{Time.now.to_i}"
#organization = Organization.create(:name => params[:user][:organization][:name], :access_code => #access_code)
#user.organization_id = #organization.id
#user.is_admin = true
else
# try and add someone to an organization
#organization = Organization.find(:all, conditions: ["name = ? AND access_code = ?", params[:user][:organization][:name], params[:user][:organization][:access_code]])
if #organization.empty?
flash.now[:error] = "No organization has been found with that name and access code."
render :new
return
else
#user.organization_id = #organization.first.id
end
end
if #user.save
user = login(#user.email, params[:user][:password])
if user
flash[:success] = "Your account has been successfully created!"
redirect_to admin_dashboard_path
end
else
flash.now[:error] = "Something went wrong! Please try again."
render :new
end
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
if #user.is_admin?
if params[:user][:organization][:name].blank? && params[:user][:organization][:name] != #user.organization.name
params[:user][:organization][:name] = #user.organization.name
end
if params[:user][:organization][:access_code].blank? && params[:user][:organization][:access_code] != #user.organization.access_code
params[:user][:organization][:access_code] = #user.organization.access_code
end
#organization = Organization.find(params[:user][:organization_id])
#organization.name = params[:user][:organization][:name]
#organization.access_code = params[:user][:organization][:access_code]
#organization.save
end
if #user.update(user_params)
flash[:success] = "Your settings have been updated!"
redirect_to edit_admin_user_path(#user.id)
else
flash.now[:error] = "Something went wrong! Please try again."
render :edit
end
end
private
def user_params
params.require(:user).permit(:organization_id, :email, :password, :password_confirmation, :full_name, :remember_me, {:organization_attributes => [:name, :website, :description, :access_code]})
end
users.rb
class User < ActiveRecord::Base
authenticates_with_sorcery!
belongs_to :organization
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates_presence_of :full_name
validates_presence_of :email
validates_uniqueness_of :email, :on => :create
validates_format_of :email, :with => VALID_EMAIL_REGEX, :on => :create
validates_presence_of :password, :on => :create
validates_confirmation_of :password
end
organization.rb
class Organization < ActiveRecord::Base
authenticates_with_sorcery!
has_many :users, :dependent => :destroy
accepts_nested_attributes_for :users
validates_presence_of :name
end
new.html.erb
<% provide(:title, 'Create a User') %>
<h1>Create a User</h1>
<p>Use the form below to create an account.</p>
<%= nested_form_for([:admin, #user], html: {role: "form"}) do |f| %>
<%= render "shared/error_messages", obj: #user %>
<fieldset>
<legend>User Information</legend>
<div class="form-group">
<%= f.label :full_name, "Full Name" %>
<span class="help-block">How should others see you?</span>
<%= f.text_field :full_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<span class="help-block">Your email address is used as your login.</span>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation, class: "form-control" %>
</div>
</fieldset>
<%= f.fields_for :organization do |o| %>
<fieldset>
<legend>Associated Organization</legend>
<div class="form-group">
<%= o.label :name, "Organization Name" %>
<span class="help-block">This is the name of the organization you are a part of.</span>
<%= o.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= o.label :access_code, "Organization Access Code" %>
<span class="help-block">Leaving this field blank will setup a new organization.</span>
<%= o.text_field :access_code, class: "form-control" %>
</div>
</fieldset>
<% end %>
<div class="form-actions">
<%= f.submit "Create Account", class: "btn btn-primary" %>
<%= link_to "Cancel", :back, class: "text-btn" %>
</div>
<% end %>
edit.html.erb
<% provide(:title, "Edit User: #{#user.full_name} (#{#user.organization.name})") %>
<h1>Edit User: <%= #user.full_name %> (<%= #user.organization.name %>)</h1>
<p>Use the form below to manage your account.</p>
<%= nested_form_for([:admin, #user], html: {role: "form"}) do |f| %>
<%= render "shared/error_messages", obj: #user %>
<fieldset>
<legend>User Information</legend>
<div class="form-group">
<%= f.label :full_name, "Full Name" %>
<span class="help-block">How should others see you?</span>
<%= f.text_field :full_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<span class="help-block">Your email address is used as your login.</span>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, placeholder: "leave blank to keep password unchanged", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation, class: "form-control" %>
</div>
</fieldset>
<% if #user.is_admin? %>
<%= f.fields_for :organization do |o| %>
<fieldset>
<legend>Associated Organization</legend>
<div class="form-group">
<%= o.label :name, "Organization Name" %>
<span class="help-block">This is the name of the organization you are a part of.</span>
<%= o.text_field :name, class: "form-control", value: #user.organization.name %>
</div>
<div class="form-group">
<%= o.label :access_code, "Organization Access Code" %>
<span class="help-block">Leaving this field blank will setup a new organization.</span>
<%= o.text_field :access_code, class: "form-control", value: #user.organization.access_code %>
</div>
</fieldset>
<% end %>
<%= f.hidden_field :organization_id %>
<% end %>
<div class="form-actions">
<%= f.submit "Update User", class: "btn btn-primary" %>
<%= link_to "Cancel", :back, class: "text-btn" %>
</div>
<% end %>
Ok, those are all the files making it happen. Now, i have the application doing almost everything i need it to do but this doesn't feel like production-level code to me.
One issue i know i am having is that if a user types something in the organization field and nothing else the controller will create and save the organization and then render the form back with the user validation errors. I don't want it to save the organization if there are validation errors in the user model.
I'm really just asking for advice if there is a better way of doing what i am trying to do. If you can't tell exactly what i'm trying to do with this code or have any questions please let me know!
Take a look at this post: http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Of particular interest will be the section on "3. Extract Form Objects".

rails: "Sign In" and "Password Authentication" of Users

Im trying to set up a log-in and password page for my site. This will be on the index page. I have a Model Class: "User" and a controller class "users_controller".
The behavior that Im trying to implement is this:
Users type in the url of the site and it opens the index page as shown below. Then the users signs in and is brought to a different page where they have access to the content of the site.
Now the question is what do I need to put in the "def index" function so that it accepts the form's paramters and passes these to another function called "self.authenticate(parameters)" to check and return a status
Here is the code for the users_controller
class UsersController < ApplicationController
def index
#user = User.new
respond_to do |format|
format.html
#index.html.erb
end
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def edit
#user = User.find(params[:id])
#title = #user.name
end
def destroy
#user = User.find(params[:id])
end
def new
#user = User.new
#title = "Sign Up"
end
def create
end
end
Here is the form:
<html>
<h1>Returning Users- Sign In</h1>
<%= form_for(#user) do |f| %>
<div class="field">
<%=f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class = "field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class = "actions">
<%= f.submit "Sign In" %>
</div>
<% end %>
<h1>New Users- Sign Up</h1>
<%= form_for(#user) do |f| %>
<div class="field">
<%=f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%=f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class = "field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class = "field">
<%= f.label :password_confirmation, "Confirm Password" %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class = "actions">
<%= f.submit "Sign Up" %>
</div>
<%end%>
<br/>
<%= debug(params) if Rails.env.development? %>
<br/>
</body>
Any help would be great. Thanks
Have a look at the recent RailsCast Authentication from Scratch from Ryan Bates. You can also use a common authentication plugin like AuthLogic or Devise.
I'm going to explain to you what you want to do.
You can get the params by using the hash params:
params[]
Then, you can get the value of a field by passing it's identification, like:
params[:id]
or
params[:name]
Now pass it to the self.authenticate(parameters) in the way you can(I don't know how this method is implemented).

Resources