无法访问rails

时间:2015-11-29 08:32:12

标签: html ruby-on-rails

目前我直接访问我的联接表。我这样做的原因是因为我只希望工作人员能够调整status

即将面临

的问题
undefined method order_task_path' for #<#<Class:0x00000009bf62c0>:0x0000000a6d7c70>`

带参数参数:

{"id"=>"1,1"}

我可以根据需要显示联接表。问题是当我尝试在编辑中显示它时。

这是我的ordertask控制器

&#13;
&#13;
class OrdersTasksController < ApplicationController
    before_action :set_status, only: [:show]
    def index
        @orders = Order.all
        #@status = OrderTask.includes(:task,:order).where(order_id: params[:id])
    end
    def edit
        
        @status = OrderTask.find(params[:id])
    end
    def show  
        
    end
    
    
    def update
    respond_to do |format|
      if @status.update(order_params)
        format.html { redirect_to @status, notice: 'Order was successfully updated.' }
        format.json { render :show, status: :ok, location: @order }
      else
        format.html { render :edit }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
    end 
    
    private
    # Use callbacks to share common setup or constraints between actions.
   def set_status
       @status = OrderTask.includes(:task).where(order_id: params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
        params.require(:order_task,:order).permit(:id,:order_id,:status)
    end
    
end
&#13;
&#13;
&#13;

我的show.html.erb

&#13;
&#13;
<p id="notice"><%= notice %></p>


<table class="table table-hover">
    <tr>
        <td><h4>Order Number : <%= @status.first.order.order_number %></h4>
        </td>
    </tr>
    
    <tr>
        <td>Task
        </td>
        <td>Status:
        </td>
    </tr>
    <tr>
        <% @status.each do |i| %>
        <td><%= i.task.task_name %>
        </td>
        <td><%= i.status %>
        </td>
        <td><%= link_to 'Edit', edit_orders_task_path(i) %></td>
        
    </tr>
    <% end %>
</table>

<%= link_to 'Back', orders_tasks_path %>
&#13;
&#13;
&#13;

我的_form.html.erb

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

      <ul>
      <% @status.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<table class="table table-hover">
  
    <tr>
        <td><%= f.label "List of task" %>
        </td>
        <td><%= f.label "Status" %>
        </td>
    </tr>
    <tr>
        <td><%= f.task_id %>
        </td>
        <td><div class="dropdown">
            <%= f.select(:status,['In progress', 'Completed'], {}, {class: "control"})%> </div>
        </td>
        
    </tr>
    <tr>
        <td>
        </td>
        <td><%= f.submit %>
        </td>
    </tr>
</table>


<% end %>
&#13;
&#13;
&#13;

和我的routes

&#13;
&#13;
resources :orders 
    resources :services
    resources :tasks
    resources :customers
    resources :staffs
    resources :orders_tasks 
    
    root 'staffs#index'
&#13;
&#13;
&#13;

最后是我的ordertask型号

&#13;
&#13;
class OrderTask < ActiveRecord::Base
    self.primary_key = [:order_id,:task_id]
    self.table_name = "Orders_tasks"
    belongs_to :order
    belongs_to :task
    

end
&#13;
&#13;
&#13;

希望你们能提出建议或其他方面的帮助。 thx提前。

修改

这是我的route.rb

&#13;
&#13;
C:\Users\Idea\DHMS>rake routes
          Prefix Verb   URI Pattern                      Controller#Action
          orders GET    /orders(.:format)                orders#index
                 POST   /orders(.:format)                orders#create
       new_order GET    /orders/new(.:format)            orders#new
      edit_order GET    /orders/:id/edit(.:format)       orders#edit
           order GET    /orders/:id(.:format)            orders#show
                 PATCH  /orders/:id(.:format)            orders#update
                 PUT    /orders/:id(.:format)            orders#update
                 DELETE /orders/:id(.:format)            orders#destroy
        services GET    /services(.:format)              services#index
                 POST   /services(.:format)              services#create
     new_service GET    /services/new(.:format)          services#new
    edit_service GET    /services/:id/edit(.:format)     services#edit
         service GET    /services/:id(.:format)          services#show
                 PATCH  /services/:id(.:format)          services#update
                 PUT    /services/:id(.:format)          services#update
                 DELETE /services/:id(.:format)          services#destroy
           tasks GET    /tasks(.:format)                 tasks#index
                 POST   /tasks(.:format)                 tasks#create
        new_task GET    /tasks/new(.:format)             tasks#new
       edit_task GET    /tasks/:id/edit(.:format)        tasks#edit
            task GET    /tasks/:id(.:format)             tasks#show
                 PATCH  /tasks/:id(.:format)             tasks#update
                 PUT    /tasks/:id(.:format)             tasks#update
                 DELETE /tasks/:id(.:format)             tasks#destroy
       customers GET    /customers(.:format)             customers#index
                 POST   /customers(.:format)             customers#create
    new_customer GET    /customers/new(.:format)         customers#new
   edit_customer GET    /customers/:id/edit(.:format)    customers#edit
        customer GET    /customers/:id(.:format)         customers#show
                 PATCH  /customers/:id(.:format)         customers#update
                 PUT    /customers/:id(.:format)         customers#update
                 DELETE /customers/:id(.:format)         customers#destroy
          staffs GET    /staffs(.:format)                staffs#index
                 POST   /staffs(.:format)                staffs#create
       new_staff GET    /staffs/new(.:format)            staffs#new
      edit_staff GET    /staffs/:id/edit(.:format)       staffs#edit
           staff GET    /staffs/:id(.:format)            staffs#show
                 PATCH  /staffs/:id(.:format)            staffs#update
                 PUT    /staffs/:id(.:format)            staffs#update
                 DELETE /staffs/:id(.:format)            staffs#destroy
    orders_tasks GET    /orders_tasks(.:format)          orders_tasks#index
                 POST   /orders_tasks(.:format)          orders_tasks#create
 new_orders_task GET    /orders_tasks/new(.:format)      orders_tasks#new
edit_orders_task GET    /orders_tasks/:id/edit(.:format) orders_tasks#edit
     orders_task GET    /orders_tasks/:id(.:format)      orders_tasks#show
                 PATCH  /orders_tasks/:id(.:format)      orders_tasks#update
                 PUT    /orders_tasks/:id(.:format)      orders_tasks#update
                 DELETE /orders_tasks/:id(.:format)      orders_tasks#destroy
            root GET    /                                staffs#index
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

根据错误消息undefined method order_task_path' for #<#<Class:0x00000009bf62c0>:0x0000000a6d7c70>,url helper方法不存在。

您可以在终端中搜索rake routes以获取所有路线和路线助手。

resources :orders do resources :tasks end 生成以下路线和助手。

order_tasks GET /orders/:order_id/tasks(.:format) tasks#index POST /orders/:order_id/tasks(.:format) tasks#create new_order_task GET /orders/:order_id/tasks/new(.:format) tasks#new edit_order_task GET /orders/:order_id/tasks/:id/edit(.:format) tasks#edit order_task GET /orders/:order_id/tasks/:id(.:format) tasks#show PATCH /orders/:order_id/tasks/:id(.:format) tasks#update PUT /orders/:order_id/tasks/:id(.:format) tasks#update DELETE /orders/:order_id/tasks/:id(.:format) tasks#destroy

resources :orders do resources :orders_tasks end

生成以下路线和助手。

order_orders_tasks GET /orders/:order_id/orders_tasks(.:format) orders_tasks#index POST /orders/:order_id/orders_tasks(.:format) orders_tasks#create new_order_orders_task GET /orders/:order_id/orders_tasks/new(.:format) orders_tasks#new edit_order_orders_task GET /orders/:order_id/orders_tasks/:id/edit(.:format) orders_tasks#edit order_orders_task GET /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#show PATCH /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#update PUT /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#update DELETE /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#destroy

http://guides.rubyonrails.org/routing.html#nested-resources