Fahad Idrees
Fahad Idrees

Reputation: 188

routes in Ruby on Rails 4

Good Day, i'm having a trouble with the routes in Ruby on Rails 4

Error:

undefined method `routes_path'

View:

<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 %>

Controller:

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

Model:

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

Routes

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

Views, controller and model are named "Route"

Is a problem with the route in the view or something else? Thank you

Upvotes: 1

Views: 204

Answers (2)

Vlad Khomich
Vlad Khomich

Reputation: 5880

Matt (the previous answer author) pretty much answered the question, just want to notice that you can also append the as option to your route to give it a name:

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

this will "define" the routes_path for you.

Upvotes: 1

Matt
Matt

Reputation: 14048

Just as a first impression, without looking into it in detail - you may have trouble using routes as a class name, it's already a class name under ActionDispatch.

However, I think your problem is actually your route:

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

This isn't a resource route, it won't generate the kind of functionality that allows you to use the form tag syntax <%= form_for @route...

Either define routes as a resource:

resources :routes

Or define a url in your form:

<%= form_for @route, :url => some_url, :html => { :multipart => true } do %>  

Upvotes: 3

Related Questions