Activity
@foliwe
how would you create an edit page with a form to edit the user profile
Assuming you are using devise and your models look something like this:
class Profile < ApplicationRecord
belongs_to :user
end
# and
class User < ApplicationRecord
has_one :profile, dependent: :destroy
after_create :init_profile
def init_profile
self.create_profile!
end
end
Then in your profiles_controller.rb
:
def edit
@profile = current_user.profile
end
def update
@profile = current_user.profile
respond_to do |format|
if @profile.update profile_params
format.html { redirect_to edit_profile_path, notice: "Profile updated!" }
format.json { render :edit, status: :ok, location: @profile }
else
format.html { redirect_to edit_profile_path, flash: { error: "Profile could not be updated!" } }
format.json { render json: @profile.errors.messages, status: :unprocessable_entity }
end
end
end
then in views/profiles/edit.erb
:
<%= form_for @profile, url: {action: "update"} do |f| %>
<!-- Whatever your fields are -->
<%= f.text_field :name %>
<%= f.text_area :bio %>
<%= f.submit "Update Profile" %>
<% end %>
Using @profile = current_user.profile
instead of @profile = Profile.find(params[:id])
means that you always get the currently logged in user's profile when they go to an edit view, so they can't get anyone else's. It also sets up the form to already know which user's profile to update. If anyone has a reason not to do this, let me know!
ArgumentError: wrong number of arguments (given 1, expected 2..3)
/Users/nicknoble/products/Lnky/db/migrate/20180531013954_convert_to_active_storage.rb:14:inprepare'
up'
/Users/nicknoble/products/Lnky/db/migrate/20180531013954_convert_to_active_storage.rb:14:in
This is awesome, but is there a way to do it, get a profile.id
and still be able to do something like this later:
# profile.rb
validates_presence_of :name, :bio
I figured it out (not perfect, but it works):
@owner = @project.project_users.where( role: "owner" ).first.user
@excid3:disqus
How would you need to modify this to work with deeply nested resources?
i.e:
resources :projects do
resources :project_users, path: :users, module: :projects
resources :posts do
resources :comments, module: :posts
end
end
How do you actually check that role field later, i.e. vs `current_user`?