I have the following view in app/views/posts/index.html.erb:
<% @posts.each do |post| %>
<%= post.user %> is listening to <%= post.song %> by <%= post.artist %> from <%= post.album %>
<% end %>
And the corresponding controller:
class PostsController < ApplicationController
def index
@posts = Post.find(:all)
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:posts])
if @post.save
redirect_to action: "index"
else
@posts = Post.find(:all)
render action: 'new'
end
end
end
The problem is, when I create a record with the song attribute being "The Sound of Settling", the artist attribute being "Death Cab for Cutie", and the album atrribute being "Transatlanticism", nothing shows up when I render the index view.
indexcode looks like it can't go wrong. I'd suspect that your form is off somehow. (Hey, how come you're usingparams[:posts]in yourcreateaction?)(0.1ms) begin transaction SQL (144.9ms) INSERT INTO "posts" ("album", "artist", "created_at", "song", "updated_at") VALUES (?, ?, ?, ?, ?) [["album", nil], ["artist", nil], ["created_at", Tue, 25 Dec 2012 15:28:49 UTC +00:00], ["song", nil], ["updated_at", Tue, 25 Dec 2012 15:28:49 UTC +00:00]