Contact name not showing name in DB - ruby-on-rails

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' %>

Related

Type error on nested form

been banging my head on the table over this one. I have another nested form but this one is driving me crazy.
The error is:
TypeError in CompaniesController#create
no implicit conversion of Symbol into Integer
#company = Company.new(company_params)
The controller:
class CompaniesController < ApplicationController
layout 'welcome'
def new
#company = Company.new
end
def create
#company = Company.new(company_params)
if #company.save
flash[:notice] = "New company created successful."
redirect_to admin_accounts_path
else
flash.now[:alert] = "Creation failed, please try again"
render :new
end
end
private
def company_params
params.require(:company).permit(:name, :location, :users_attributes => [:email, :password])
end
end
On the new.html.erb:
<%= render partial: 'form', locals: { company: #company, users_attributes: :users_attributes } %>
This code looks exactly like another nested setup I have, but it works :p
I had read that sometimes changing the params from => to just having a semicolon works, but replacing the user_attributes => with a user_attributes: didn't change anything.
EDIT: form.html.erb
<%= form_for company, url: companies_path do |f| %>
<div class="col-12">
<div class="row">
<div class="col p-0 mr-3">
<div class="form-group">
Company <%= f.label :name %>
<%= f.text_field :name, :placeholder => 'Company name', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :location %>
<%= f.text_field :location, :placeholder => 'Location', class: 'form-control' %>
</div>
</div>
<div class="col p-0">
<%= f.fields_for users_attributes do |user_f| %>
<div class="form-group">
<%= user_f.label :email %>
<%= user_f.text_field :email, :placeholder => 'Your Email Address', class: 'form-control' %>
</div>
<div class="form-group">
<%= user_f.label :password %>
<%= user_f.password_field :password, :placeholder => 'Password', class: 'form-control' %>
</div>
<% end %>
</div>
<div class="col-12 p-0">
<%= f.submit "Sign-Up", :class => 'btn btn-primary btn-block btn-lg' %>
</div>
</div>
</div>
<% end %>
Use users instead of users_attributes on the form.
Don't forget to define accepts_nested_attributes_for :users in model Company
## new.html.erb
<%= render partial: 'form', locals: { company: #company, users_attributes: :users } %>
I wonder why you didn't put :users into the form directly

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

Showing :name from signup form in profiles form using Devise in rails app

I need my App to be able to display :name from the sign up form in the profiles/new _form is that possible? and how?
When my User sign up for an account via Devise they sign up by inserting :name, emailand password. Like seen in the code below:
<div class="row img-back">
<div class="col-md-6 col-md-offset-3 text-center">
<div class="well sign-up-form">
<h2>Ný skráning</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field form-group">
<%= f.label :Nafn_fyrirtækis_stofnunar %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :tölvu_póstfang %><br />
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :Lykilorð %>
<% if #minimum_password_length %>
<em>(Minnst <%= #minimum_password_length %> stafir )</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :Staðfesting_lykilorðs %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
</div>
<div class="actions">
<%= f.submit "Ný skrá", class: 'btn btn-success' %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
After Sign up the user is directed to the views/profiles/new.html.erb with this method in registrations_controller.rb
def after_sign_up_path_for(resource)
new_user_profile_path(current_user)
end
And in the views/profiles/new.html.erb the user fills in a _form.html.erb partial to finish its profile and register all the information the app needs to work for the user.
this is the _form.html.erb
%= form_for #profile, url: user_profile_path, :html => { :multipart => true } do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :staff %>
<%= f.number_field :staff, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :building_size %>
<%= f.number_field :building_size, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :postalcode %>
<%= f.text_field :postalcode, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :website%>
<%= f.text_field :website, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :contact_person %>
<%= f.text_field :contact_person, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :contact_email %>
<%= f.email_field :contact_email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :buisness_type %>
<%= f.select :buisness_type,['Þjónusta', 'Ráðgjöf', 'Verlsun', 'Stofnun', 'Annað'], class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Update Profile", class: 'btn btn-primary' %>
</div>
<% end %>
My profile.rb belongs_to :userand my user.rb model has_one :profile
EDIT according to jsw324 answer
Here is the profiles_controller.rb
class ProfilesController < ApplicationController
before_action :authenticate_user!
#before_action :only_current_user
def new
#form where a user can fill put their pwn profile.
#user = User.find(params[:user_id]) # Finnur hver er userinn sem er loggaður inn, reaching in to the url and grabbs user.
#profile = Profile.new
#profile.name = current_user.name
end
def show
#user = User.find(params[:user_id])
##profile = #user.profile
#profile.name = current_user.name
end
def create
#user = User.find(params[:user_id])
#profile = #user.build_profile(profile_params)
#Lecture 159 User show action next :))))
if #profile.save
flash[:success] = "Profile updated"
redirect_to user_path(params[:user_id]) #fer á user/show.html.erb
else
render action: :new
end
end
def edit
#user = User.find(params[:user_id])
#profile = #user.profile
end
def update
#user = User.find(params[:user_id])
#profile = #user.profile
if #profile.update_attributes(profile_params)
flash[:success] = "Profile Updated!"
redirect_to user_path(params[:user_id])
else
render action :edit
end
end
private
def profile_params
params.require(:profile).permit(:name, :staff, :address, :postalcode, :website, :contact_person, :contact_email, :phone_number, :buisness_type, :description, :building_size, :avatar)
end
def only_current_user
#user = User.find(params[:user_id])
redirect_to(root_url) unless #user == current_user
end
And I edited this line in the views/profiles/_form.html.erb
<%= form_for #profile, url: user_profile_path, :html => { :multipart => true } do |f| %>
<div class="row">
<div class="container">
<div class="col-md-6">
<div class="form-group">
<%= f.label :Name %>
<%= f.name %>
</div>
Now I get this Error: undefined methodname' for #`
Ahh. You need to handle that in the controller. Something like #profiles.name = current_user.name.
I'm not sure I understand but to display the name you can just use <%= current_user.name %> wherever you need it.

Update feature does not update post but creates a new post

I've been creating a system where I create these entries but the problem is when I tried to create the edit feature, it does not update the post but creates a new one.
My controller file looks like this:
class BetsController < ApplicationController
def new
#bet = Bet.new
end
def create
#bets = Bet.new(bet_params)
if #bets.save
flash[:success] = 'Bet Successfull Logged.'
redirect_to new_bet_path
else
flash[:danger] = 'Error, Bet has not been logged. Try again mate.'
redirect_to new_bet_path
end
end
def show
#bet = Bet.find(params[:id])
end
def edit
#bet = Bet.find(params[:id])
end
def update
#bet = Bet.find(params[:id])
if #bet.update_attributes(bet_params)
flash[:success] = "Bet Updated!"
redirect_to bet_path(params[:id])
else
render action: :edit
end
end
private
def bet_params
params.require(:bet).permit(:bet_placed, :game, :units_placed, :odds, :profit_or_loss)
end
end
And also the form that is submitting looks like this:
<%= form_for :bet, url: bets_path, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :game %><br>
<%= f.text_field :game, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :units_placed %><br>
<%= f.text_field :units_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :odds %><br>
<%= f.text_field :odds, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>
The way your form is currently setup, it will always hit the create action. If you change your form_for to the following it should work
<%= form_for #bet do |f| %>
...
<% end %>
You shouldn't need multipart => true either since your form doesn't have any file inputs.
According to the rails natural routing the path bets_path always goes to the create method. As update needs an id, and you are not giving it, it goes to create.
Change the form to,
<%= form_for #bet, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
....................
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>
For reference,
HTTPVerb Path controller#Action Named Helper
GET /bets bets#index bets_path
GET /bets/new bets#new new_bet_path
POST /bets bets#create bets_path
GET /bets/:id bets#show bet_path(:id)
GET /bets/:id/edit bets#edit edit_bet_path(:id)
PATCH/PUT /bets/:id bets#update bet_path(:id)
DELETE /bets/:id bets#destroy bet_path(:id)

My forum is not updating the table?

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

Resources