嵌套资源轨道

时间:2016-07-08 11:04:59

标签: ruby-on-rails nested-resources

我有两个模型,即待办事项和任务。在他们之间设置了一个Has_many,Belongs_to assosciation(todo有很多任务,任务属于todo)。

我在它们之间设置了一个嵌套资源,如下面的路线文件

所示
resources :todos do
    resources :tasks
  end

我创建新任务的表单如下所示:

<%= form_for([@todo, @task], html:{class:'form-horizontal'}) do |f| %>
  <% if @task.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@task.errors.count, "error") %> prohibited this todo from being saved:</h2>

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

  <script type="text/javascript">
  $(document).ready(function () {
          $('#datep').on('change', function () {
          $('.datepicker').hide();
      });

  });
  </script>

  <div class="container">
      <table style="width:70%" class="table">
        <tr>
          <td><%= f.label :title , "Task Title"%>
          <%= f.text_field :title,:class=>'form-control input-sm' %></td>
        </tr>

         <tr>
           <td><%= f.label :description,"Task Description"%>
           <%= f.text_field :description,:class=>'form-control input-sm' %></td>
        </tr>

        <tr>
          <td><%= f.label :deadline,"Task DeadLine"%>
          <h id="datep"><%= f.text_field :deadline, :class=>'form-control input-sm', "data-provide" => "datepicker" ,"data-date-format" => "mm-dd-yyyy"%></h></td>
        </tr>

        <tr>
          <td><%= f.label :assignee,"Task Assignee" %><br/>
          <%= f.collection_select :assignee, User.all, :name, :name, {}, :class => "btn btn-default dropdown-toggle"%></td>
        </tr>

        <tr>
          <td><%= f.label :tags,"Tags for Task"%>
          <%= f.text_field :tags,:class=>'form-control input-sm' %></td>
        </tr>

        <tr>
          <td><%= f.label :state,"Task Status"%><br/>
          <%= f.select :state, ['Yet To Start', 'In Progress', 'Finished'], {}, :class => "btn btn-default dropdown-toggle"%></td>
        </tr>

    </table>
  </div>

  <br />
  <div class="actions form-group">
    <div class="container">
    <%= f.submit 'Create New Task', class:'btn btn-success'%>
    &nbsp;&nbsp;
    <%= link_to todos_path, class: 'btn btn-warning' do %>
      <i class="glyphicon glyphicon-hand-left"></i> Back To List
    <% end %>
    </div>

  </div>
  <% end %>

我的任务控制器代码如下所示:

class TasksController < ApplicationController

  def new
    @task = Task.new
  end

  def create
    @todo = Todo.find.(params[:todo_id])
    @task = @todo.tasks.build(task_params)
    if @task.save
      redirect_to [@todo, @task]
    else
      redirect_to root_path
    end
  end

 private

  def set_todo
    @todo = Todo.find(params[:todo_id])
  end

  def task_params
    params.require(:task).permit(:assignee, :deadline, :state, :tags, :title, :description)
  end

end

但是,当我想向特定待办事项添加新任务时,我会收到此错误

undefined method `tasks_path' for #<#<Class:0x007fd16a9c8810>:0x007fd169c95a80>
Did you mean?  asset_path

需要帮助才能知道我在这里做错了什么???

1 个答案:

答案 0 :(得分:0)

redirect_to只接受哈希,记录或字符串,因此此行redirect_to [@todo, @task]无法正常工作。见http://api.rubyonrails.org/classes/ActionController/Redirecting.html。您可以将其更改为redirect_to todo_tasks_path(@todo),这在这里是合理的。

我也注意到你没有把before_filter :set_todo放在控制器中。您可以在@todo = Todo.find.(params[:todo_id])操作中移除create,因为它不再需要。

希望它有所帮助。

相关问题