How to show user's first_name in page like "Hello, current_user.first_name", my schema
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "first_name", default: "", null: false
t.string "last_name", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
when i tried to do in my page <span>Hello, <%= current_user.email %></span> it's all ok, but whet i tried <span>Hello, <%= current_user.first_name %></span> it's show nothing. Where is my mistake?
The problem here is in your schema, you have t.string "first_name", default: "", null: false which saves the empty string in your database, if you don't pass the first_name.I'm quite sure you are not saving the first_name value to user anyhow and it is by default set to "". When you try to access <span> <%= "Hello, #{current_user.first_name}" %></span> here it return empty string.Here user is present as if user is not present then it would have given error as first_name for nil:NilClass.
add this to your ApplicationController
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
permit_attrs(%i[first_name last_name])
end
def permit_attrs(attrs)
%i[sign_up account_update].each do |action|
devise_parameter_sanitizer.permit(action, keys: attrs)
end
end
in your new.html.erb you have two first name field change one as below:
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: false, class: "form-control" %>
</div>
to
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autofocus: false, class: "form-control" %>
</div>
So if you are seeking to display user information, this is how I handle that.
Display User Input Fields
<p> Hello <%= #user.first_name %>
Now, if you want to be able to show user input information IF they have it.
If User Filled Out Form
<% if #user.first_name? %>
<p>Hello, <%= #user.first_name %></p>
<% else %>
<p>Hello Anonymous User</p>
<% end %>
Related
I've added a checkbox to my Rails project. When the user marks tradeablestatus while creating a book, it should write true, but it is happening false regardless.
books/_form.html.erb
<%= form_with(model: book, local: true) do |form| %>
<% if book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p> If you want your book to be available, tick the status and if you want your book to be tradeable, tick the tradeablestatus. </p>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :author %>
<%= form.text_field :author %>
</div>
<div class="field">
<%= form.label :pagecount %>
<%= form.number_field :pagecount %>
</div>
<div class="field">
<%= form.label :status %>
<%= form.check_box :status %>
</div>
<div class="field">
<%= form.label :tradeablestatus %>
<%= form.check_box :tradeablestatus %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
tradeablestatus migration (I added the tradeablestatus later as a migration. But I don't know if I should add this to book migration)
class AddTradeableStatusToBooks < ActiveRecord::Migration[6.0]
def change
add_column :books, :tradeablestatus, :boolean
end
end
schema.rb (I suspect it is caused by default: false, null: false)
ActiveRecord::Schema.define(version: 2020_10_28_092816) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "books", force: :cascade do |t|
t.string "title"
t.string "author"
t.integer "pagecount"
t.boolean "status"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "user_id"
t.string "username"
t.boolean "tradeablestatus", default: false, null: false
end
create_table "comments", force: :cascade do |t|
t.string "title"
t.string "content"
t.bigint "user_id", null: false
t.bigint "book_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "status", default: false, null: false
t.index ["book_id"], name: "index_comments_on_book_id"
t.index ["user_id"], name: "index_comments_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "username"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "comments", "books"
add_foreign_key "comments", "users"
end
Hello) 👋 If u want to receive "true" or "false" on your back-end side u should check this first https://apidock.com/rails/ActionView/Helpers/FormHelper/check_box.
There are smth like check_box("puppy", "gooddog", {}, "yes", "no").
Yes and no means that if checkbox are checked then yes value goes to back-end side otherwise no goes to back-end side. U should replace this values with yours)
If problem will not go then post your controller code please)
Building a Rails app using devise. The user can edit all the information on their profile page (name, description etc.) inside devise/edit.html.erb. The user model has an association to language model. I want the user to be able to pick their own language when editing their profile from a f.select. How do I make this happen?
Thanks in advance.
Edit.html.erb:
<div class="panel-body">
<div class="panel-body">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div>
<div class="form-group">
<%= f.label :coach_id %>
<%= f.select(:coach_id, User.where('is_coach = ?', true).pluck(:name, :id), {}, { :class => 'select2 form-control' }) %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control', rows: '5' %>
</div>
<div class="form-group">
<%= f.label :facebook %>
<%= f.text_field :facebook, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :twitter %>
<%= f.text_field :twitter, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :header_image %>
<%= f.file_field :header_image, class: 'form-control' %>
</div>
</div>
<br>
<%= f.submit 'Update Settings', class: 'btn btn-success' %>
<% end %>
</div>
</div>
Schema:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin", default: false
t.boolean "is_coach", default: false
t.text "description"
t.string "image_uid"
t.string "header_image_uid"
t.string "facebook"
t.string "twitter"
t.boolean "featured"
t.integer "coach_id"
t.string "name"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
create_table "languages", force: :cascade do |t|
t.string "language_code"
t.integer "user_id"
t.index ["user_id"], name: "index_languages_on_user_id", using: :btree
end
create_table "languages_users", force: :cascade do |t|
t.integer "user_id"
t.integer "language_id"
t.index ["language_id"], name: "index_languages_users_on_language_id", using: :btree
t.index ["user_id"], name: "index_languages_users_on_user_id", using: :btree
end
User
has_and_belongs_to_many :languages
Language
has_and_belongs_to_many :users
I have two model
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :personal
before_create :generate_profile
end
and personal.rb
class Personal < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
end
and in my controller i have :
def createinfo
#user = current_user
#personal = #user.personal
end
I want to do a form for like that :
<%= form_for #personal do |f| %>
Trigramme : <%= f.text_field :trigramme, :input_html => { :value => 'blabla' } %><br />
Nom : <%= f.text_field :nom %><br />
Prenom : <%= f.text_field :prenom %><br />
Poste : <%= f.text_field :poste %><br />
Bio : <%= f.text_area :bio %><br />
<%= f.submit %>
<% end %>
But i have an error and I don't know how to resove that. I think the path current_user.personal is not accepted by form_for although it print the correct thing when I only put current_user.personal.trigramme in the code.
undefined method `personal_path' for #<#<Class:0x8e15a28>:0x8e15308>
do you know how can I do this form?
EDIT : My route.rb
Rails.application.routes.draw do
devise_for :users
root 'static_pages#cvpage'
get 'home' => 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'cvpage' => 'static_pages#cvpage'
get 'createinfo' => 'static_pages#createinfo'
end
EDIT 2 schema.rb :
create_table "personals", force: :cascade do |t|
t.string "trigramme"
t.string "nom"
t.string "prenom"
t.string "poste"
t.text "bio"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "personals", ["user_id"], name: "index_personals_on_user_id"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
YEAHH! I solve my problem (take a lot of time)
I put in my personal controller this :
def update
#user = current_user
#personal = #user.personal
if #personal.update_attributes(personal_params)
# if update is successful
else
# if update is unsuccessful
end
redirect_to home_path
end
def personal_params
params.require(:personal).permit(:trigramme, :nom, :prenom, :poste, :bio)
end
And i put in my view this form :
<%= form_for(#personal, html: { method: :put }) do |f| %>
Trigramme : <%= f.text_field :trigramme %><br />
Nom : <%= f.text_field :nom %><br />
Prenom : <%= f.text_field :prenom %><br />
Poste : <%= f.text_field :poste %><br />
Bio : <%= f.text_area :bio %><br />
<%= f.submit %>
Thank you to all of you !!
I am trying to add several images to my site and am not quite sure how the routing works.
Here is my index.html.erb page
<%= render 'pages/home' unless user_signed_in? %>
<div id="properties" class="transitions-enabled">
<% #properties.each do |property| %>
<div class="box panel panel-default">
<%= link_to image_tag(property.image.url(:medium)), property_path(property) %>
<div class="panel-body">
<%= property.description %><br>
<strong><%= property.user.name if property.user %></strong>
<!-- <% if property.user == current_user %>
<div class="actions">
<%= link_to 'Edit', edit_property_path(property) %>
<%= link_to 'Destroy', property, method: :delete, data: { confirm: 'Are you sure?' } %>
</div> -->
<% end %>
</div>
</div>
<% end %>
</div>
And I think it's after this line <%= link_to image_tag(property.image.url(:medium)), property_path(property) %> that I want to add another image.
Here is my _formfield.html.erb file where I ask for two images
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :street_image %><br>
<%= f.file_field :street_image, class: "form-control" %>
</div>
How do I show the :streetimage properly?
And this is my property model:
class Property < ActiveRecord::Base
belongs_to :user
has_attached_file :image, :styles => { :medium => "300x300", :thumb => "100x100"}
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
validates :image, presence: true
validates :description, presence: true
end
And here's my schema
ActiveRecord::Schema.define(version: 20150429054455) do
create_table "properties", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "name"
t.string "street"
t.integer "zip"
t.string "state"
t.string "company"
t.string "city"
t.integer "price"
t.integer "space"
t.string "street_image_file_name"
t.string "street_image_content_type"
t.integer "street_image_file_size"
t.datetime "street_image_updated_at"
end
add_index "properties", ["user_id"], name: "index_properties_on_user_id"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
It's working now. Looks like I had the wrong variable set and didn't run rake db:migrate before
I am new to Rails, and I am working with Devise. The problem I am facing is that my form is not updating the name column of the user when they are signing up. I have the name attribute in the data table, but I can't seem to alter it using a form in devise registration.
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :name %><br />
<%= f.text_field :name, :autofocus => true %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
end
Scheme.rb
ActiveRecord::Schema.define(version: 20130622203624) do
create_table "posts", force: true do |t|
t.integer "user_id"
t.text "description"
t.integer "comments_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
create_table "views", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", limit: 128, default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "views", ["email"], name: "index_views_on_email", unique: true, using: :btree
add_index "views", ["reset_password_token"], name: "index_views_on_reset_password_token", unique: true, using: :btree
end
If you are using rails4 you need strong parameters. If you need to implement extra parameters to devise, you could either create an additional 1-1 relation called profiles for users and store them there (better in the long run of app or if you have more user data) and I personally feel it to be much easier the incorporating your user data in the devise user table.
Alternatively you could do the following if you are using one or two attributes.
You need to permit the name parameter for devise. in your ApplicationController code, do this.
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
end
end
Refer more on this on devise wiki.
Need to make the following changes - and that should fix it
gem 'protected_attributes'
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:name, :user_type,:email, :password, :password_confirmation)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:name,:user_type,:email, :password, :password_confirmation, :current_password)
end
end