如何将一个模型表单字段调用到另一个控制器索引?

时间:2015-07-16 04:55:05

标签: ruby-on-rails

Todos Controller

    class TodosController < ApplicationController
  # GET /todos
  # GET /todos.json
  def index
    @todos = Todo.all
    @projects = Project.new

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @todos }

    end
  end

  # GET /todos/1
  # GET /todos/1.json
  def show
    @todo = Todo.find(params[:id])

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

  # GET /todos/new
  # GET /todos/new.json
  def new
    @todo = Todo.new

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

  # GET /todos/1/edit
  def edit
    @todo = Todo.find(params[:id])
  end

  # POST /todos
  # POST /todos.json
  def create
    @todo = Todo.new(params[:todo])

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

  # PUT /todos/1
  # PUT /todos/1.json
  def update
    @todo = Todo.find(params[:id])

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

  # DELETE /todos/1
  # DELETE /todos/1.json
  def destroy
    @todo = Todo.find(params[:id])
    @todo.destroy

    respond_to do |format|
      format.html { redirect_to(todos_url) }
      format.json  { render :json => {} }
    end
  end
  def newproject
       @projects = Project.all       
    end
end

Todos_form.html.erb

    <%= form_for(@todo) do |f| %>
  <% if @todo.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo.errors.count, "error") %> prohibited this todo from being saved:</h2>

      <ul>
      <% @todo.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_field :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

projects_form.html.erb

 <%= form_for(@project) do |f| %>
  <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

      <ul>
      <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_area :name %>
  </div>
  <div class="field">
    <%= f.label :project_id %><br />
    <%= f.number_field :project_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

project.rb

   class Project < ActiveRecord::Base
  attr_accessible :name, :project_id
  has_many :todos
  def as_json(options = {})
    super(options.merge(:only => [ :id, :name, :project_id]))
  end
end

todo.rb

    class Todo < ActiveRecord::Base
  attr_accessible :content, :order, :done
  belongs_to :project

  def as_json(options = {})
    super(options.merge(:only => [ :id, :content, :order, :done ]))
  end
end

嗨我有两个模型Todos和Projects,在Todos索引中我想显示项目字段值。怎么可能帮助我如何进行。 我也需要协会。

注意:字段值必须来自项目控制器并保存为数据库。

1 个答案:

答案 0 :(得分:2)

第一个项目模型不应该有project_id列。 todo模型中应该存在project_id

然后改变路线。

resources :projects do
  resources :todos
end

现在将代码添加到项目控制器。

class ProjectsController < ApplicationController
  def index
    @projects = Project.all
  end

  def show
    @project = Project.find(params[:id])
    @todos = @project.todos.all
  end

  def new
    @project = Project.mew
  end

  def create
    @project = Project.new(params[:project])

    if @project.save
      .....
    else
      ....
    end
  end
end

个别项目包含自己的待办事项。因此,在项目显示页面中,您可以显示与项目关联的所有待办事项。 现在todo控制器应该看起来像:

class TodosController < ApplicationController
  def new
    @project = Project.find(params[:project_id])
    @todo = @project.todos.new
  end

  def create
    @project = Project.find(params[:project_id])
    @todo = @project.todos.build(params[:todo])

    if @todo.save
      .....
    else
      ....
    end
  end

  def show
    @project = Project.find(params[:project_id])
    @todo = @project.todos.find(params[:id])
  end
end

最后在app/views/projects/new.html.erb文件中添加以下代码:

<%= form_for @project do |f| %>
  <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

      <ul>
        <% @project.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_area :name %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

app/views/todos/new.html.erb中添加代码:

<%= form_for @todo, url: project_todos_path(@project), method: :post do |f| %>
  <% if @todo.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo.errors.count, "error") %> prohibited this todo from being saved:</h2>

      <ul>
        <% @todo.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_field :content %>
  </div>

 <div class="actions">
   <%= f.submit %>
 </div>