2

I'm using form_for to create a chatroom and when I view the page I get the following error:

NoMethodError in Chatrooms#new 
undefined method `chatrooms_path' for #<#<Class:0xa862b94>:0xa5307f0>

Here's the code for the view, located in app/views/chatrooms/new.html.erb:

<div class="center">
  <%= form_for(@chatroom) do |f| %>
    <%=f.text_field :topic%>
    <br>
    <%=f.submit "Start a discussion", class: "btn btn-large btn-primary"%>
  <% end %>
</div>

Here's the relevant controller:

class ChatroomsController < ApplicationController
  def new
    @chatroom = Chatroom.new
  end

  def show
    @chatroom = Chatroom.find(params[:id])
  end
end

If I change the line

<%= form_for(@chatroom) do |f| %>

to

<%= form_for(:chatroom) do |f| %>

it works fine.
I've searched around for similar questions but none of the solutions have worked for me. Help?

2
  • do you have resources :chatrooms in your routes.rb? Commented Nov 25, 2012 at 10:02
  • Please post your routes.rb and/or the result of rake routes. Commented Nov 25, 2012 at 11:02

1 Answer 1

1

It is because you didn't create route/action for ChatroomsController. When you render new form it is pointing to create action by default, if you want to change handler action, use

form_for @chatroom, :url => some_other_path
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.