"NoMethodError" on /messages/new on content variable


#1

Whenever I go to messages/new I get an error saying NoMethodError for the content variable.


#2

Hi @zleitzke,

Could you please post your code from app/controllers/messages_controller.rb please?

Thanks :slight_smile:


#3
`class MessagesController < ApplicationController
def index
	@messages = Message.all
end
def new
    @message = Message.new
end
def create
    @message = Message.new(message_params)
if @message.save
  redirect_to '/messages'
else
  render 'new'
end
end
private
  def message_params

params.require(:message).permit(:content)

end
end`

#4

@zleitzke That looks OK aside from indentation. Could you post app/views/messages/new.html.erb, please?

Thanks :slight_smile:


#5
<div class="header">
  <div class="container">
    <img src="http://s3.amazonaws.com/codecademy-content/courses/learn-rails/img/logo-1m.svg">
    <h1>Messenger</h1>
  </div>
</div>

<div class="create">
  <div class="container">
    
    <!-- Your code here -->
    <%= form_for(@message) do |f| %>
    <div class="field">
      <%= f.label :message %><br>
      <%= f.text_area :content %>
    </div>
    <div class="actions">
      <%= f.submit "Create" %>
    </div>
    <% end %>
  </div>
</div>

#6

@zleitzke Hm, I'm not quite sure what the problem is :confused:

Could I see your db/migrate file for the Message model?


#7
class CreateMessages < ActiveRecord::Migration
  def change
    create_table :messages do |t|
      t.text :content
      t.timestamps null: false
    end
  end
end

#8

@zleitzke I'm not sure this will solve the problem, but mine looks different:

class CreateMessages < ActiveRecord::Migration
  def change
    create_table :messages do |t|
      t.text :content
      t.timestamps
    end
  end
end

Could you try removing the null: false line?

Also, is this on Create Messages I or Create Messages II?


#9

Still does not work.


#10

@zleitzke Which exercise are you on?


#11

A post was split to a new topic: “NoMethodError” on /messages/new


#12

This topic is inactive.