0

我一直在努力让这个工作,以便当前登录的用户只能查看他们发布的内容。我想我几乎已经破解了它,但是我遇到了一个未定义的方法“每个”错误。

这是我编辑的仅有的两个文件:

_form.html.erb

<h1>Listing programs</h1>

<table>

<% @programs.each do |program| %>
  <tr>
    <td><h2><%= program.title %></h2></td>
  </tr>
  <tr>
    <td><%= program.body %></td>
  </tr>
  <tr>
    <td><%= link_to 'Show', program %> | <%= link_to 'Edit', edit_program_path(program) %> | <%= link_to 'Destroy', program, method: :delete, data: { confirm: 'Are you sure?' } %>    </td>
  </tr>

<% end %>
</table>

<br />

<%= link_to 'New Program', new_program_path %>

程序控制器.rb

class ProgramsController < ApplicationController

  before_filter :authenticate_coach!, :except => [:show]


  # GET /programs
  # GET /programs.json

  def index
    @programs = Program.find(current_coach[:id])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @programs }
    end
  end

  # GET /programs/1
  # GET /programs/1.json
  def show
    @program = Program.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @program }
    end
  end

  # GET /programs/new
  # GET /programs/new.json
  def new
    @program = Program.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @program }
    end
  end

  # GET /programs/1/edit
  def edit
    @program = Program.find(params[:id])
  end

  # POST /programs
  # POST /programs.json
  def create
    @program = Program.new(params[:program])


    respond_to do |format|
      if @program.save
        format.html { redirect_to @program, notice: 'Program was successfully created.' }
        format.json { render json: @program, status: :created, location: @program }
      else
        format.html { render action: "new" }
        format.json { render json: @program.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /programs/1
  # PUT /programs/1.json
  def update
    @program = Program.find(params[:id])

    respond_to do |format|
      if @program.update_attributes(params[:program])
        format.html { redirect_to @program, notice: 'Program was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @program.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /programs/1
  # DELETE /programs/1.json
  def destroy
    @program = Program.find(params[:id])
    @program.destroy

    respond_to do |format|
      format.html { redirect_to programs_url }
      format.json { head :no_content }
    end
  end
end

我目前拥有的不同模型Program是教练将发布的模型和Coach(用户):

程序.rb:

class Program < ActiveRecord::Base

  belongs_to :coach

  validates_presence_of :title
  attr_accessible :body, :title, :coach_id



end

教练.rb:

class Coach < ActiveRecord::Base

  has_many :programs

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :coach
  # attr_accessible :title, :body


end

如果您需要更多详细信息,请告诉我,我会给您。

4

1 回答 1

0

利用

@programs = Program.find_all_by_coach_id(current_coach[:id])

或者我想你也应该改变

current_coach[:id]

current_coach.id
于 2013-06-21T21:43:26.787 回答