ruby on rails - Only users that created their own project can see the edit page and actually edit -


is there easy way allow users created own project able edit work?

class project < activerecord::base   belongs_to :user end  class user < activerecord::base   has_many :projects end 

how check if current_user logged in can edit stuff?

if projects url's localhost:3000/projects/24, want user created project can go localhost:3000/projects/24/edit view page , edit...


at time of writing, i'm thinking might not best way? maybe need somehow localhost:3000/projects/username1/24 or something? , if edit, it'll localhost:3000/projects/username1/24/edit.... how can accomplish this?

my routes:

rails.application.routes.draw   devise_for :users   'users/:id' => 'users#show', as: :user   resources :projects end 

my controller basic stuff scaffolding

maybe need somehow localhost:3000/projects/username1/24 or something? , if edit, it'll localhost:3000/projects/username1/24/edit.... how can accomplish this?

since user has many projects, want url of type:

localhost:3000/users/1/projects/2/edit 

to accomplish need following setup:

#routes.rb  resources :users, shallow: true # notice shallow, eliminate users/ path, i'm not mistaken /user_id/ well..   resources :projects end 

the controller projects should put under:

app/controllers/users/projects_controller.rb 

and should like

class users   class projects     #...   end end 

with setup you'll ensure, user see projects.

def index   @projects = projects.all # user can see projects   #... end  def show   @project = projects.find(params[:project_id])   #... end  def edit   @project = current_user.projects.find(params[:project_id]) # can edit those, associated   # ... end 

and make sure making link_to edit visible user can edit.

as paths:

there 2 options:

resources :users, path: '' # hide users part path   #... end resources :users, shallow: true # hide users/user_id part   #... end 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -