Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. First argument in form cannot contain nil or be empty
  2.  
  3. error messages partial
  4.  
  5. <% if object.errors.any? %>
  6. <div id="error_explanation">
  7. <div class="alert alert-danger">
  8. The form contains <%= pluralize(object.errors.count, "error") %>
  9. </div>
  10. <ul>
  11. <% object.errors.full_messages.each do |msg| %>
  12. <li><%= msg %></li>
  13. <% end %>
  14. </ul>
  15. </div>
  16. <% end %>
  17.  
  18. comment form
  19.  
  20. <%= form_for @comment, url: comments_path do |f| %>
  21. <%= render 'shared/error_messages', object: f.object %>
  22. <%= f.hidden_field :user_id, value: current_user.id %>
  23. <%= f.hidden_field :post_id, value: post.id %>
  24. <%= f.text_area :content, size: "60x2", placeholder: "Comment on this post..." %>
  25. <%= f.submit "Comment" %>
  26. <% end %>
  27.  
  28. post form
  29.  
  30. <%= form_for [@user, @post] do |f| %>
  31. <%= render 'shared/error_messages', object: f.object %>
  32. <%= f.text_area :content, size: "60x12", placeholder: "What do you want to say?" %>
  33. <%= f.submit "Post" %>
  34. <% end %>
  35.  
  36. class CommentsController < ApplicationController
  37. def index
  38. @comments = Comment.all
  39. end
  40.  
  41. def new
  42. @comment = Comment.new
  43. @user = User.find(params[:user_id])
  44. end
  45.  
  46. def create
  47. @user = current_user
  48. #@post = Post.find(params[:post_id])
  49. @comment = @user.comments.build(comment_params)
  50.  
  51. if @comment.save
  52. flash[:success] = "Comment Posted!"
  53. redirect_back(fallback_location: root_path)
  54. else
  55. flash[:notice] = "Could not post comment"
  56. redirect_back(fallback_location: root_path)
  57. end
  58. end
  59.  
  60.  
  61. private
  62.  
  63. def comment_params
  64. params.require(:comment).permit(:content, :user_id, :post_id)
  65. end
  66. end
  67.  
  68. class PostsController < ApplicationController
  69.  
  70. def index
  71. @posts = Post.all
  72. @user = User.find(params[:user_id])
  73. end
  74.  
  75. def new
  76. @post = Post.new
  77. @user = User.find(params[:user_id])
  78. end
  79.  
  80. def create
  81. @post = current_user.posts.build(post_params)
  82. if @post.save
  83. flash[:success] = "Posted!"
  84. redirect_to user_path(current_user)
  85. else
  86. flash[:notice] = "Post could not be submitted"
  87. redirect_to users_path
  88. end
  89. end
  90.  
  91. private
  92.  
  93. def post_params
  94. params.require(:post).permit(:content)
  95. end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement