user100051
user100051

Reputation:

Having trouble with Rails Routing after form submission

So I'm trying to make a form that would allow users to comment on specific posts. Im currently having trouble after the submit button is clicked. The entry does get put into the database, however it looks like im experiencing some routing problems.

Here is the URL I get redirected to: "localhost:3000/groups/13/posts/62/comments"

I get the following error after submission:

No route matches {:action=>"show", :controller=>"groups"}

I ran rake routes to find this:

         group GET    /groups/:id(.:format)           groups#show

Here is my comments controller:

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].merge({:user_id => current_user.id}))
    redirect_to :action => :show, :controller => :groups
  end
end

Here is my form for the comment:

                    <%= form_for([post.group, post, post.comments.build]) do |f| %>
                      <p>
                        <%= f.label :comment %><br />
                        <%= f.text_area :body, :rows => 3, :cols => 55 %>
                      </p>
                      <p>
                        <%= f.submit %>
                      </p>
                    <% end %>

Does anyone have any ideas what might be wrong? Why is it redirecting to the url "localhost:3000/groups/13/posts/62/comments"

Thanks

Upvotes: 0

Views: 95

Answers (1)

Robin
Robin

Reputation: 21894

I would do:

class CommentsController < ApplicationController
    respond_to :html

    def create
        @post = Post.find(params[:post_id])
        @comment = @post.comments.create(params[:comment]) do |comment|
            comment.user = current_user # user_id shouldn't be an attr_accessible
        end
        respond_with @comment, location: group_path(@post.group)
    end

Upvotes: 1

Related Questions