Daði Hall Daði Hall - 2 months ago 11
Ruby Question

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
,
email
and
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 :user
and 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 method
name' for #`

Answer

Ahh. You need to handle that in the controller. Something like @profiles.name = current_user.name.