Erick Sitter

Joined

540 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Updating Forum_Posts getting routes error

I eventually got it working, I just called the controller and passed (@forum_thread, forum_post)

Posted in let users edit their own posts

I currently have it setup to where only the admin can edit posts, but would like if the user can edit their posts but no other posts that were not made by them.

Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread

def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

def edit
@forum_thread = ForumThread.friendly.find(params[:forum_thread_id])
@forum_post = ForumPost.find(params[:id])
@forum_post = current_user.forum_posts.find(params[:id])
end

def update
@forum_post = ForumPost.find(params[:id])
if @forum_post.update(forum_post_params)
redirect_to @forum_thread
else
render 'edit'
end
end

def destroy
@forum_post = ForumPost.find(params[:id])
@forum_post.destroy
redirect_to @forum_thread
end

private

def set_forum_thread
  @forum_thread = ForumThread.friendly.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end

HTML

<%= div_for @forum_post do %>

55), :class => "img-circle avatar" %>

Posted by

<% end %>

Posted in Updating Forum_Posts getting routes error

app/views/forum_posts/_forum_post.html.erb

<%= div_for @forum_post do %>

65), :class => "img-circle avatar" %>

Posted by

<% end %>

Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread

def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

def edit
@forum_post.user = current_user
@forum_post = ForumPost.find(params[:id])
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_thread
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end

Log

ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"forum_threads/forum_posts", :forum_thread_id=># 2:

<%= image_tag forum_post.user(:size => 65), :class => "img-circle avatar" %>

3:

4:

Posted by <%= forum_post.user %> <%= local_time_ago forum_post.created_at %>
5: <%= link_to edit_forum_thread_forum_post_path(@forum_post), class: "btn btn-danger btn-xs" do %>
6:
7: <% end %>
8:


app/views/forum_posts/forum_post.html.erb:5:in `block in _app_views_forum_postsforum_post_html_erb2837216844227051161_43160880'
app/views/forum_posts/_forum_post.html.erb:1:in `_app_views_forum_posts
forum_post_html_erb_2837216844227051161_43160880'
app/views/forum_threads/show.html.erb:10:in `_app_views_forum_threads_show_html_erb
312355849593620906_29108500'

Posted in Checking if Forum_Thread is updating?

The forum_threads model doesn't have :forum_thread_id but forum_posts does.

I have the find :foumd_thread_id in the posts model.

Posted in Checking if Forum_Thread is updating?

Now the actual update is simple it's just @forum_thread.update params and redirect, but as you can see it redirects to the forum_thread which renders the show.html.erb which then causes @forum_post to create a new post which is causing duplicate entries after updating...

I thought a simple check with @forum_thread.update //nothing here else // new forum post end would solve it, no... just causes argument errors.

Also the user data isn't being supplied when updating, I have added @forum_post.user = current_user in the update but does nothing...

Forum Thread Controller

Forum Threads Controller

class ForumThreadsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_forum_thread, except: [:index, :new, :create]

  def index
    @q = ForumThread.search(params[:q])
    @forum_threads = @q.result(distinct: true)
  end

  def show
   if @forum_thread_updating = true
     # Nothing happens when updating
   else
     @forum_post = ForumPost.new
   end
  end

  def new
    @forum_thread = ForumThread.new
    @forum_thread.forum_posts.new
  end

  def create
    @forum_thread = current_user.forum_threads.new forum_thread_params
    @forum_thread.forum_posts.first.user_id = current_user.id

    if @forum_thread.save
      redirect_to @forum_thread
    else
      render action: :new
    end
  end

  def edit
    @forum_thread_updating = true
    @forum_post.user = current_user
  end

  def update
     if @forum_thread.update forum_thread_params and @forum_post.user = current_user
        redirect_to @forum_thread
     else
        render 'edit'
     end
  end

  def destroy
    @forum_thread.destroy
    redirect_to root_path
  end

  private

    def set_forum_thread
      @forum_thread = ForumThread.find(params[:id])
      @forum_post = ForumPost.find(params[:id])
    end

    def forum_thread_params
      params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body], forum_posts_attributes.where([:id =>]))
    end
end

Posted in Devise no current_user data after update

Forum Threads Controller

class ForumThreadsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]

def index
@q = ForumThread.search(params[:q])
@forum_threads = @q.result(distinct: true)
@forum_threads = ForumThread.paginate(:page => params[:page], :per_page => 3)
end

def show
@forum_posts = ForumThread.find(params[:id])
@forum_post.user = current_user
@forum_post = ForumPost.new
@forum_posts = ForumPost.paginate(:page => params[:page], :per_page => 3)
end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
@forum_thread.forum_posts.first.user_id = current_user.id

if @forum_thread.save
redirect_to @forum_thread
else
render action: :new
end
end

def edit
end

def update
if @forum_thread.update forum_thread_params
redirect_to @forum_thread
else
render 'edit'
end
end

def destroy
@forum_thread.destroy
redirect_to root_path
end

private

def set_forum_thread
@forum_thread = ForumThread.find(params[:id])
end
Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread

def new
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user
end

def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user
if @forum_post.save
redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
redirect_to @forum_thread, alert: "Unable to save your post"
end
end

def edit
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_thread
else
render 'edit'
end
end

private

def set_forum_thread
@forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
params.require(:forum_post).permit(:body)
end
end
log

I, [2015-11-18T06:16:17.418239 #16523] INFO -- : Started GET "/forum/forum_threads/17" for 24.220.125.144 at 2015-11-18 06:16:17 -0600
I, [2015-11-18T06:16:17.453154 #16523] INFO -- : Processing by ForumThreadsController#show as HTML
I, [2015-11-18T06:16:17.453292 #16523] INFO -- : Parameters: {"id"=>"17"}
D, [2015-11-18T06:16:17.479120 #16523] DEBUG -- : [[1m[[36mForumThread Load (0.5ms)[[0m [[1mSELECT forum_threads.* FROM forum_threads WHERE forum_threads.id = $
D, [2015-11-18T06:16:17.487690 #16523] DEBUG -- : [[1m[[35mCACHE (0.0ms)[[0m SELECT forum_threads.* FROM forum_threads WHERE forum_threads.id = 17 LIMIT 1 [["i$
D, [2015-11-18T06:16:17.503484 #16523] DEBUG -- : [[1m[[36mUser Load (0.9ms)[[0m [[1mSELECT users.* FROM users WHERE users.deleted_at IS NULL AND users.id $
D, [2015-11-18T06:16:17.541778 #16523] DEBUG -- : [[1m[[35mUser Load (0.5ms)[[0m SELECT users.* FROM users WHERE users.deleted_at IS NULL AND users.id = 9 L$
D, [2015-11-18T06:16:17.573968 #16523] DEBUG -- : [[1m[[36mForumPost Load (0.6ms)[[0m [[1mSELECT forum_posts.* FROM forum_posts WHERE forum_posts.forum_thread_id$
D, [2015-11-18T06:16:17.579587 #16523] DEBUG -- : [[1m[[35mCACHE (0.0ms)[[0m SELECTusers.* FROMusersWHEREusers.deleted_atIS NULL ANDusers.id= 9 LIMIT$
I, [2015-11-18T06:16:17.583134 #16523] INFO -- : Rendered forum_posts/_forum_post.html.erb (7.0ms)
I, [2015-11-18T06:16:17.583329 #16523] INFO -- : Rendered forum_threads/show.html.erb within layouts/application (48.1ms)
I, [2015-11-18T06:16:17.583711 #16523] INFO -- : Completed 500 Internal Server Error in 130ms (ActiveRecord: 6.8ms)
F, [2015-11-18T06:16:17.585545 #16523] FATAL -- :
ActionView::Template::Error (undefined methodemail' for nil:NilClass):
1: <%= div_for forum_post do %>
2:

<%= image_tag current_user.gravatar_url(:size => 65), :class => "img-circle avatar" %><$
3:

4:
Posted by <%= forum_post.user.email %> <%= local_time_ago forum_post.created_at %>

5:
<%= forum_post.body %>

6:

7: <% end %>
app/views/forum_posts/forum_post.html.erb:4:in block in app_views_forumpostsforum_post_html_erb2875746267982681747_45837340'
app/views/forum_posts/forum_post.html.erb:1:in
app_views_forum_postsforum_post_html_erb_2875746267982681747_45837340'
app/views/forum_threads/show.html.erb:6:in `app_views_forum_threadsshow_html_erb3506623455216328957_43396520'

when I make edit's it comes up as Posted by datetime, does not even display #User:0x0000000579c610,
I tried passing @forum_user.post

Posted in Devise: current_user not saving to @forum_post.user

I'm currently having an issue when I create posts, I always seem to get undefined method "username" or "email", but when using current_user.username or current_user.email it will display just fine...

Forum Threads Controller

class ForumThreadsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]

def index
@q = ForumThread.search(params[:q])
@forum_threads = @q.result(distinct: true)
@forum_threads = ForumThread.paginate(:page => params[:page], :per_page => 3)
end

def show
@forum_posts = ForumThread.find(params[:id])
@forum_post.user = current_user
@forum_post = ForumPost.new
@forum_posts = ForumPost.paginate(:page => params[:page], :per_page => 3)
end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
@forum_thread.forum_posts.first.user_id = current_user.id

if @forum_thread.save
redirect_to @forum_thread
else
render action: :new
end
end

def edit
end

def update
if @forum_thread.update forum_thread_params
redirect_to @forum_thread
else
render 'edit'
end
end

def destroy
@forum_thread.destroy
redirect_to root_path
end

private

def set_forum_thread
@forum_thread = ForumThread.find(params[:id])
end
Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread

def new
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user
end

def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user
if @forum_post.save
redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
redirect_to @forum_thread, alert: "Unable to save your post"
end
end

def edit
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_thread
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end
log

I, [2015-11-18T06:16:17.418239 #16523] INFO -- : Started GET "/forum/forum_threads/17" for 24.220.125.144 at 2015-11-18 06:16:17 -0600
I, [2015-11-18T06:16:17.453154 #16523] INFO -- : Processing by ForumThreadsController#show as HTML
I, [2015-11-18T06:16:17.453292 #16523] INFO -- : Parameters: {"id"=>"17"}
D, [2015-11-18T06:16:17.479120 #16523] DEBUG -- : [[1m[[36mForumThread Load (0.5ms)[[0m [[1mSELECT forum_threads.* FROM forum_threads WHERE forum_threads.id = $
D, [2015-11-18T06:16:17.487690 #16523] DEBUG -- : [[1m[[35mCACHE (0.0ms)[[0m SELECT forum_threads.* FROM forum_threads WHERE forum_threads.id = 17 LIMIT 1 [["i$
D, [2015-11-18T06:16:17.503484 #16523] DEBUG -- : [[1m[[36mUser Load (0.9ms)[[0m [[1mSELECT users.* FROM users WHERE users.deleted_at IS NULL AND users.id $
D, [2015-11-18T06:16:17.541778 #16523] DEBUG -- : [[1m[[35mUser Load (0.5ms)[[0m SELECT users.* FROM users WHERE users.deleted_at IS NULL AND users.id = 9 L$
D, [2015-11-18T06:16:17.573968 #16523] DEBUG -- : [[1m[[36mForumPost Load (0.6ms)[[0m [[1mSELECT forum_posts.* FROM forum_posts WHERE forum_posts.forum_thread_id$
D, [2015-11-18T06:16:17.579587 #16523] DEBUG -- : ^[[1m^[[35mCACHE (0.0ms)^[[0m SELECT
users.* FROMusersWHEREusers.deleted_atIS NULL ANDusers.id= 9 LIMIT$
I, [2015-11-18T06:16:17.583134 #16523] INFO -- : Rendered forum_posts/_forum_post.html.erb (7.0ms)
I, [2015-11-18T06:16:17.583329 #16523] INFO -- : Rendered forum_threads/show.html.erb within layouts/application (48.1ms)
I, [2015-11-18T06:16:17.583711 #16523] INFO -- : Completed 500 Internal Server Error in 130ms (ActiveRecord: 6.8ms)
F, [2015-11-18T06:16:17.585545 #16523] FATAL -- :
ActionView::Template::Error (undefined method
email' for nil:NilClass):
1: <%= div_for forum_post do %>
2:

<%= image_tag current_user.gravatar_url(:size => 65), :class => "img-circle avatar" %><$
3:

4:

Posted by <%= forum_post.user.email %> <%= local_time_ago forum_post.created_at %>


5:

<%= forum_post.body %>


6:

7: <% end %>
app/views/forum_posts/forum_post.html.erb:4:in `block in _app_views_forum_postsforum_post_html_erb2875746267982681747_45837340'
app/views/forum_posts/_forum_post.html.erb:1:in `_app_views_forum_posts
forum_post_html_erb_2875746267982681747_45837340'
app/views/forum_threads/show.html.erb:6:in `_app_views_forum_threads_show_html_erb
3506623455216328957_43396520'

Posted in Updating forum thread creates new post...

Forum Thread Controller

class ForumThreadsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]

def index
@q = ForumThread.search(params[:q])
@forum_threads = @q.result(distinct: true)
@forum_threads = ForumThread.paginate(:page => params[:page], :per_page => 3)
end

def show
@forum_post = ForumPost.new
@forum_posts = ForumThread.find(params[:id])
@forum_posts = ForumPost.paginate(:page => params[:page], :per_page => 3)
end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
@forum_thread.forum_posts.first.user_id = current_user.id

if @forum_thread.save
  redirect_to @forum_thread
else
  render action: :new
end

end

def edit
end

def update
if @forum_thread.update forum_thread_params
redirect_to @forum_thread
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:id])
end

def forum_thread_params
  params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end

end

Posted in No action "edit" in forum_posts_controller

Forum_Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

def edit
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_thread
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
  @forum_post = ForumPost.find(params[:id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end

HTML



<%= div_for forum_post do %>

Posted by <%= forum_post.user.email %> <%= time_ago_in_words forum_post.created_at %> <%= link_to "Edit", :controller => "forum_posts", :action => "edit", :id => forum_post.id %>


<%= forum_post.body %>


<% end %>

Posted in undefined method username after update

Forum_threads Controller

class ForumThreadsController < ApplicationController
require 'forum_controller'
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]

def index
@q = ForumThread.search(params[:q])
@forum_threads = @q.result(distinct: true)
end

def show
@forum_post = ForumPost.new
@forum_posts = @forum_thread.forum_posts.paginate(:page => params[:page], :per_page => 2)
end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
@forum_thread.forum_posts.first.user_id = current_user.id

if @forum_thread.save
  redirect_to @forum_thread
else
  render action: :new
end

end

def edit
end

def update
if @forum_thread.update(forum_thread_params)
redirect_to @forum_thread
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:id])
  @forum_post = ForumPost.find(params[:id])
end

def forum_thread_params
  params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end

end

Error Log

ActionView::Template::Error (undefined method `username' for nil:NilClass):
1:



2: <%= div_for forum_post do %>
3:

Posted by <%= forum_post.user.username %> <%= time_ago_in_words forum_post.created_at %>


4:

<%= forum_post.body %>


5: <% end %>

If I remove the forum_post.user.username from the html it'll load just fine, I've tried adding @forum_post to the update but that didn't work either...

Posted in Forum_posts edit coming up as nil

Thanks, got it!

But had to move it to edit.

Posted in Forum_posts edit coming up as nil

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
1: <%= form_for [@forum_thread, @forum_post] do |f| %>
2:

Reply to thread


3:

4: <%= f.text_area :body, placeholder: "Add a comment", rows: 10, class: "form-control"$
app/views/forum_posts/form.html.erb:1:in `_app_views_forum_postsform_html_erb_3822141534$
app/views/forum_threads/forum_posts/edit.html.erb:2:in `_app_views_forum_threads_forum_posts
$

Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), not$
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

def edit
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_post
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end

Posted in Number of posts on index / Posts pagination

Got it correctly working by @forum_posts = @forum_thread.forum_posts.paginate
But all the replies/posts are still showing and not limiting to 3 per page...

Posted in Number of posts on index / Posts pagination

I followed your forum series and it's going to work out well for me, but I couldn't figure out how to display the number of posts on the main index for each forum thread.

I'd think it would have been as simple as just parsing forum_thread.forum_posts in html.
It works, but just get a bunch of activerecord collection proxy errors.

I also wanted to paginate the posts and limit to 10 posts per page, now I can get pagination showing but it appears to be collecting all of the posts not from the specific forum thread.

All the code.

Forum_threads Controller:
class ForumThreadsController < ApplicationController
require 'forum_controller'
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]

def index
@q = ForumThread.search(params[:q])
@forum_threads = @q.result(distinct: true)
@forum_threads = ForumThread.paginate(:page => params[:page], :per_page => 3)
end

def show
@forum_post = ForumPost.new
@forum_posts = ForumThread.find(params[:id])
@forum_posts = ForumPost.paginate(:page => params[:page], :per_page => 3)
end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
@forum_thread.forum_posts.first.user_id = current_user.id

if @forum_thread.save
  redirect_to @forum_thread
else
  render action: :new
end

end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:id])
end

def forum_thread_params
  params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end

end

Forum_Posts_Controller:
class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), notice: "Successfully posted!"
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end

Routes:
Rails.application.routes.draw do
devise_for :users, :path => '', :path_names => {:sign_up => 'register', :sign_in => 'login', :sign_out => 'logout'}
scope "/forum" do
resources :forum_threads do
resources :forum_posts, module: :forum_threads
end
end
end

Also I would like to reroute as of right now it's /forum/forum_threads/id, which is fine when you're viewing the post but to view the thread index it's /forum/forum_index/ I'd prefer to re-route that index without creating a controller to just /forum/ then to the posts can still be /forum/forum_threads/id.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.