Ask A Question

Notifications

You’re not receiving notifications from this thread.

Updating Forum_Posts getting routes error

Erick Sitter asked in General

app/views/forum_posts/_forum_post.html.erb

<%= div_for @forum_post do %>

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

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

<%= forum_post.body %>

<% 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'

Reply

Try to run rake routes and double check you have the correct route name. And it doesn't look like you allowing the user_id in your strong params. Try changing to:

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

And I'm not 100% positive but I'm pretty sure you don't have to reset the user in your edit method since you're assigning it when it's created. Might cause an issue if someone other than the original user tries to edit the post.
Hope this helps. :)

Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.