Как мне показать все этапы (одна модель), которые принадлежат проекту (другая модель) в Rails 3? - PullRequest
0 голосов
/ 25 ноября 2010

У меня два контроллера: проекты и этапы.

Проекты имеют много этапов и этапов принадлежит проектам. Я хочу, чтобы при нажатии на название проекта (т. Е. Проекты показывают действие), чтобы он отображал все этапы, связанные с этим проектом.

Как мне это сделать?

Весь соответствующий код можно найти ниже:

Stages Controller

class StagesController < ApplicationController
  filter_resource_access

  # GET /stages
  # GET /stages.xml
  def index
    @stages = Stage.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @stages }
    end
  end

  # GET /stages/1
  # GET /stages/1.xml
  def show
    @stage = Stage.find(params[:id])
    #@project = Project.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @stage }
    end
  end

  # GET /stages/new
  # GET /stages/new.xml
  def new
    @stage = Stage.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @stage }
    end
  end

  # GET /stages/1/edit
  def edit
    @stage = Stage.find(params[:id])
  end

  # POST /stages
  # POST /stages.xml
  def create
    @stage = current_user.stages.create(params[:stage])
    #@stage = Stage.new(params[:stage])

    respond_to do |format|
      if @stage.save
        format.html { redirect_to(@stage, :notice => 'Stage was successfully created.') }
        format.xml  { render :xml => @stage, :status => :created, :location => @stage }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @stage.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stages/1
  # PUT /stages/1.xml
  def update
    @stage = Stage.find(params[:id])

    respond_to do |format|
      if @stage.update_attributes(params[:stage])
        format.html { redirect_to(@stage, :notice => 'Stage was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @stage.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stages/1
  # DELETE /stages/1.xml
  def destroy
    @stage = Stage.find(params[:id])
    @stage.destroy

    respond_to do |format|
      format.html { redirect_to(stages_url) }
      format.xml  { head :ok }
    end
  end
end

Stages model:

# == Schema Information
# Schema version: 20101124095341
#
# Table name: stages
#
#  id         :integer         not null, primary key
#  project_id :integer
#  created_at :datetime
#  updated_at :datetime
#  user_id    :integer
#  name       :string(255)
#  stage_num  :integer


class Stage < ActiveRecord::Base

  belongs_to :projects
  #has_and_belongs_to_many :users

  has_many :uploads
  has_many :comments

end


Projects Model

# == Schema Information
# Schema version: 20101117094659
#
# Table name: projects
#
#  id          :integer         not null, primary key
#  name        :string(255)
#  description :string(255)
#  designer_id :integer
#  client_id   :integer
#  notified    :boolean
#  created_at  :datetime
#  updated_at  :datetime
#  user_id     :integer

class Project < ActiveRecord::Base

  belongs_to :user
  has_many :stages
  has_many :uploads
  has_many :comments

  #before_validation { |project| project.user = Authorization.current_user unless project.user }

end

Project Controller

class ProjectsController < ApplicationController
  filter_resource_access

  # GET /projects
  # GET /projects.xml
  def index
    @projects = current_user.projects

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @projects }
    end
  end

  # GET /projects/1
  # GET /projects/1.xml
  def show
    @project = Project.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end
  end

  # GET /projects/new
  # GET /projects/new.xml
  def new
    @project = Project.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @project }
    end
  end

  # GET /projects/1/edit
  def edit
    @project = Project.find(params[:id])     
  end

  # POST /projects
  # POST /projects.xml
  def create
    @project = current_user.projects.create(params[:project])

    respond_to do |format|
      if @project.save
        format.html { redirect_to(@project, :notice => 'Project was successfully created.') }
        format.xml  { render :xml => @project, :status => :created, :location => @project }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /projects/1
  # PUT /projects/1.xml
  def update
    @project = Project.find(params[:id])

    respond_to do |format|
      if @project.update_attributes(params[:project])
        format.html { redirect_to(@project, :notice => 'Project was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.xml
  def destroy
    @project = Project.find(params[:id])    
    @project.destroy

    respond_to do |format|
      format.html { redirect_to(projects_url) }
      format.xml  { head :ok }
    end
  end
end

Проекты Показать представление

<p id="notice"><%= notice %></p>

<br />
<% @projects.each do |project| %>

    <% @stages.each do |stage| %>
      <tr>
        <td><%= link_to stage.name, stage %> | </td>
        <td><%= stage.stage_num %> | </td>

        <td><%= link_to 'Show', stage %></td>
        <td><%= link_to 'Edit', edit_stage_path(stage) %></td>
        <td><%= link_to 'Destroy', stage, :confirm => 'Are you sure?', :method => :delete %></td>
      </tr>
    <% end %>

<% end %>

<% if permitted_to? :create, Stage.new %>
    <%= link_to 'New Stage', new_stage_path %>
<% end %><br /><br />

<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>

1 Ответ

1 голос
/ 25 ноября 2010

Есть много способов сделать это. Самый простой способ сделать это с помощью существующего кода - изменить страницу представления проектов.

замените это:

<% @projects.each do |project| %>

    <% @stages.each do |stage| %>

с:

<% @project.stages.each do |stage| %>

Затем вы должны использовать представление «индекс», чтобы показать все проекты, а представление «показать» - показать особенности проекта (которые будут включать этапы)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...