Guest User

Untitled

a guest
Jun 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. class Comment < ActiveRecord::Base
  2. belongs_to :post
  3. belongs_to :user
  4. end
  5. class Post < ActiveRecord::Base
  6. belongs_to :user
  7. has_many :comments
  8. end
  9. class User < ActiveRecord::Base
  10. devise :database_authenticatable, :registerable,
  11. :recoverable, :rememberable, :trackable, :validatable
  12.  
  13. attr_accessible :email, :password, :password_confirmation, :remember_me
  14. has_many :posts
  15. has_many :comments
  16. end
  17.  
  18. undefined method `email' for nil:NilClass
  19.  
  20.  
  21. 1: <p>
  22. 2: <% @post.comments.each do |comment| %>
  23. 3: <b>Comment written by:</b> <%= comment.user.email %><br />
  24. 4: <%= comment.body %><br />
  25. 5: <% end %>
  26. 6:
  27.  
  28. create_table "comments", :force => true do |t|
  29. t.integer "post_id"
  30. t.integer "user_id"
  31. t.text "body"
  32. .... truncated
  33. end
  34.  
  35. create_table "posts", :force => true do |t|
  36. t.integer "user_id"
  37. t.integer "sell_or_buy"
  38. t.string "title"
  39. t.text "body"
  40. .... truncated
  41. end
  42.  
  43. create_table "users", :force => true do |t|
  44. t.string "email", :default => "", :null => false
  45. t.string "encrypted_password", :limit => 128, :default => "", :null => false
  46. .... truncated
  47. end
  48.  
  49. class CommentsController < ApplicationController
  50. def create
  51. @post = Post.find(params[:post_id])
  52. @comment = @post.comments.create(params[:comment])
  53. @comment.user_id = current_user.id
  54. redirect_to post_path(@post)
  55. end
  56. end
  57.  
  58. def create
  59. @post = Post.find(params[:post_id])
  60. @comment = @post.comments.new(params[:comment])
  61. @comment.user_id = current_user.id
  62. @comment.save
  63. redirect_to post_path(@post)
  64. end
Add Comment
Please, Sign In to add comment