lundi 18 mai 2015

Iterate through several objects form_for rails

Executive Summary

  1. Goal: I am creating an admin dashboard, editable by a form_for
  2. I have a Post model which has two attributes: Subject and Title
  3. I have created an Admin Controller, which has an index action
  4. I get an error claiming it can't find the undefined method `subject' for ActionView:FormBuilder:0x000000031e02c8>
  5. Clearly I have a 'subject' item! I am staring at it right now... and cursing.

Dear Friends,

As you can see I have within my post.title, post.subject; I iterate through each @posts.each and then pass each individual object a form! What gives it claims i don't have an item. This is so strange?

Model: class Post < ActiveRecord::Base attr_accessor :title, :subject

end

routes:

resources :admin_dashboards

admin_dashboards_controller.rb

class AdminDashboardsController < ApplicationController

  def index
    @posts = Post.all()
  end

View: index.html.erb

<tr>
<%= @posts.each do |post| %>
<%= form_for post do |single_post| %>
<td> <%= single_post.subject %> </td>
<td> <%= single_post.submit %> </td>
</tr>
<% end %>

<% end %>

scheme.rb

  create_table "posts", force: true do |t|
    t.text     "title"
    t.text     "subject"
    t.text     "url"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Aucun commentaire:

Enregistrer un commentaire