0

Something weird is happening and I don't know why.

When I use the helper <%= link_to "New game", new_game_path %>, my new game form does not submit.

But when I acesses the view typing the URL localhost:3000/games/new form works just well

Any idea how to solve that?

Thanks,

Here my rake routes

Prefix Verb   URI Pattern                               Controller#Action
                  root GET    /                                         games#index
         user_sessions GET    /user_sessions(.:format)                  user_sessions#index
                       POST   /user_sessions(.:format)                  user_sessions#create
      new_user_session GET    /user_sessions/new(.:format)              user_sessions#new
     edit_user_session GET    /user_sessions/:id/edit(.:format)         user_sessions#edit
          user_session GET    /user_sessions/:id(.:format)              user_sessions#show
                       PATCH  /user_sessions/:id(.:format)              user_sessions#update
                       PUT    /user_sessions/:id(.:format)              user_sessions#update
                       DELETE /user_sessions/:id(.:format)              user_sessions#destroy
                 users GET    /users(.:format)                          users#index
                       POST   /users(.:format)                          users#create
              new_user GET    /users/new(.:format)                      users#new
             edit_user GET    /users/:id/edit(.:format)                 users#edit
                  user GET    /users/:id(.:format)                      users#show
                       PATCH  /users/:id(.:format)                      users#update
                       PUT    /users/:id(.:format)                      users#update
                       DELETE /users/:id(.:format)                      users#destroy
 delete_progress_progresses POST   /progresses/delete_progress(.:format)     progresses#delete_progress
            progresses POST   /progresses(.:format)                     progresses#create
                search GET    /search(.:format)                         games#search
           game_levels GET    /games/:game_id/levels(.:format)          levels#index
                       POST   /games/:game_id/levels(.:format)          levels#create
        new_game_level GET    /games/:game_id/levels/new(.:format)      levels#new
       edit_game_level GET    /games/:game_id/levels/:id/edit(.:format) levels#edit
            game_level GET    /games/:game_id/levels/:id(.:format)      levels#show
                       PATCH  /games/:game_id/levels/:id(.:format)      levels#update
                       PUT    /games/:game_id/levels/:id(.:format)      levels#update
                       DELETE /games/:game_id/levels/:id(.:format)      levels#destroy
    insert_levels_game POST   /games/:id/insert_levels(.:format)        games#insert_levels
                 games GET    /games(.:format)                          games#index
                       POST   /games(.:format)                          games#create
              new_game GET    /games/new(.:format)                      games#new
             edit_game GET    /games/:id/edit(.:format)                 games#edit
                  game GET    /games/:id(.:format)                      games#show
                       PATCH  /games/:id(.:format)                      games#update
                       PUT    /games/:id(.:format)                      games#update
                       DELETE /games/:id(.:format)                      games#destroy
                 login GET    /login(.:format)                          user_sessions#new
                logout POST   /logout(.:format)                      user_sessions#destroy

My route file

Rails.application.routes.draw do
 root :to => 'games#index'

  resources :user_sessions
  resources :users
  resources :progresses, :only => :create do 
    collection do
      post 'delete_progress'
    end
 end

 get 'search' => 'games#search'

  resources :games do
     resources :levels

     member do
      post 'insert_levels'
    end
  end

  get 'login' => 'user_sessions#new', :as => :login
  post 'logout' => 'user_sessions#destroy', :as => :logout
end
zed_0xff
  • 32,417
  • 7
  • 53
  • 72
rovitulli
  • 319
  • 3
  • 8
  • 1
    Can you explain what you mean by "my new game form does not submit"? If there is a form, you should post the code for it. – Ege Ersoz Jun 16 '14 at 04:42
  • Do you have any error? – Pavan Jun 16 '14 at 06:00
  • @enragedcamel this link_to is a link to the "new" view, with a form to create a new game. When I use the helper format, the form does not work. But If I type the URL to the same link, the form works ok. Unfortunately, I got no erros back :-( – rovitulli Jun 16 '14 at 06:03
  • @Pavan nope, no errors. – rovitulli Jun 16 '14 at 06:04
  • Post your `rake routes` output and your `routes.rb` file. – Pavan Jun 16 '14 at 06:10
  • @Pavan Here my entire project https://github.com/rovitulli/gamio – rovitulli Jun 16 '14 at 06:16
  • Why you are using `<%= link_to "New game", new_game_path %>` in application layout? You should be better put that in any of the view files then it should work fine. – Pavan Jun 16 '14 at 06:35
  • I appreciate all your help. The answer is: turbolinks :-/ I removed turbolinks and now it works just fine. – rovitulli Jun 17 '14 at 00:59

2 Answers2

0

Sure, you can use string as relative URL. This will never crash since rails will not try to resolve your routes building it. My guess is, that you might have a typo of some sort.

There is no reason why this wouldn't work. I have searched your git app for "new_game_path" but could not find a single example where you use this code.

I have only found < a href="/games/new">New game</a> in your layout.

Replace it with <%= link_to 'New Game', new_game_path %> this works in your app. I have just tested it.

If you intend to use internationalization at some point, you should avoid standard HTML links. They will not keep your locale persistent.

zed_0xff
  • 32,417
  • 7
  • 53
  • 72
Lukasz Muzyka
  • 2,783
  • 1
  • 30
  • 41
0

form

You mention that your "form does not submit"

This is not a problem with your link_to - it's an issue with your form; they are two different issues:

--

link_to

link_to takes you to a new page. It's a helper method to help create the equivalent of <a href="http://your_link.com">Your text</a>

This means that if you're sending requests to your new action, it should not matter how the users get there - only how the action is rendered.

The typical case for a form is as follows:

#app/views/games/new.html.erb
<%= form_for @game do |f| %>
   <%= f.text_field :attribute %>
   <%= f.submit "test" %>
<% end %>

#app/controllers/games_controller.rb
Class GamesController < ApplicationController
    def new
        @game = Game.new
    end
end

--

Fix

When you mention your new game form does not submit, that's an issue with your form itself. This could be due to a number of reasons, but typically with the way in which you're rendering the form

To fix this, you'll need to detail how you're rendering your form & how you'd like it to submit


Update

Having read your updated comments, if the form works when you send the requests to the "naked" url, the issue may not be with the form itself.

As a rule of thumb, you'll always want to use the Rails helpers when defining links etc. In your application layout, I found that you posted "pure" HTML to create a link. This is bad because if the Rails syntax changes, or your routes change, your application won't update correctly.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • I was testing different options in order to debug. The original code uses rails helpers. Thanks, anyway. – rovitulli Jun 16 '14 at 17:08
  • I appreciate all your help. The answer is: turbolinks :-/ I removed turbolinks and now it works just fine. – rovitulli Jun 17 '14 at 00:58