1

Ruby on Rails 4 のルートに問題があり、次のエラーが表示されます。

undefined method `routes_path'

私の見解は次のとおりです。

<h1>Load data</h1>
    <div class="row">
    <div class="span6 offset3">
    <%= form_for @route, :html => { :multipart => true } do %>
        <%= hidden_field_tag 'current_user', @current_user %>
        <%= file_field_tag :file %>
        <%= submit_tag "Import", style: 'margin-top: -10px', class: "btn btn-primary" %>
    <% end %>
</div>
</div>

私のコントローラーは:

def new
    @route = current_user.build_route
end

def create
     nil_flag = Route.import(params[:file], current_user)
    if nil_flag == 1
      flash[:success] = "Data created."
      redirect_to route_path(current_user)
    else
      flash[:error] = "Error"
      redirect_to load_data_path
    end
end 

私のモデルは次のとおりです。

def self.import(file, current_user)
   @user = current_user
   @route = @user.build_route
   @nil_flag = 0

   File.open(file.path, 'r') do |f|
   .
   .
   .
    #etc
end

ルートは次のとおりです。

match '/load_data', to: 'routes#new', via: 'get'

ビュー、コントローラー、モデルの名前は「Route」です。

ビュー内のルートに問題がありますか?

4

1 に答える 1

0

Rails がヘルパーを作成するように、ルートの名前を指定する必要があります。_path

match '/load_data', to: 'routes#new', via: 'get', as: :routes

これにより、routes_pathを返すヘルパーが作成されます/load_data

通常は、リソース ルーティングを使用する方が簡単です。

resources :routes, path: '/load_data'

上記の行は、次のルートを作成します。

ヘルパーという名前の HTTP 動詞パス アクション
GET /load_data インデックスの routes_path
GET /load_data/new 新しい new_routes_path
POST /load_data create routes_path
GET /load_data/:id show route_path(:id)
GET /load_data/:id/edit edit edit_route_path(:id)
PATCH/PUT /load_data/:id update route_path(:id)
DELETE /load_data/:id destroy route_path(:id)

ターミナルで実行rake routesして、利用可能なルートのリストを取得できます。

于 2013-10-22T21:55:40.747 に答える