Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. ## routes.rb
  2. ActionController::Routing::Routes.draw do |map|
  3. map.resources :users do |p|
  4. p.resources :posts
  5. end
  6. map.login "login", :controller => 'user_sessions', :action => 'new'
  7. map.logout "logout", :controller => 'user_sessions', :action => 'destroy'
  8. map.root :controller => 'pages'
  9. map.resources :user_sessions
  10. map.resources :password_resets
  11. map.connect ':controller/:action/:id'
  12. map.connect ':controller/:action/:id.:format'
  13. end
  14.  
  15. ## posts/_form.html.erb
  16. <% form_for([:user,@post]) do |f| %>
  17. <%= f.error_messages %>
  18. <p>
  19. <%= f.label :title %><br />
  20. <%= f.text_field :title %>
  21. </p>
  22. <p>
  23. <%= f.label :content %><br />
  24. <%= f.text_area :content %>
  25. </p>
  26. <p><%= f.submit "Submit" %></p>
  27. <% end %>
  28.  
  29. ## posts_controller.rb
  30. [...]
  31. def edit
  32. require_user
  33. @user = current_user
  34. @post = Post.find(params[:id])
  35. end
  36.  
  37. def update
  38. @post = Post.find(params[:id])
  39. if @post.update_attributes(params[:post])
  40. flash[:notice] = "Successfully updated post."
  41. redirect_to root_url
  42. else
  43. render :action => 'edit'
  44. end
  45. end
  46. [...]
  47.  
  48. ## posts/show.html.erb
  49. [...]
  50. <% if current_user.username = @user.username %>
  51. <%= link_to "Edit this post", edit_user_post_url %>
  52. <% end %>
  53. [...]
Add Comment
Please, Sign In to add comment