2
votes

I want to create player object, without views for this object model, and without additional parameters. This is action create for this object:`

def create
  @player = @tournament.players.new

  if @player.save
    redirect_to @tournament
    render :nothing => true
  end
end

This object also don't need additional parameters, because all parameters where set by default. As you can see, I tried resolve my problem with "render :nothing => true" but this don't work, and this is resoult:

Missing template players/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}.

Update:

Routes.rb

resources :users
  resources :tournaments do
    resources :players
    resources :rounds do
      resources :duels
    end
  end
end

Server logs:

Started GET "/tournaments/1/players/new" for 127.0.0.1 at 2015-11-11 17:37:00 +0100 ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by PlayersController#new as HTML Parameters: {"tournament_id"=>"1"} Tournament Load (0.5ms) SELECT "tournaments".* FROM "tournaments" WHERE "tournaments"."id" = ? LIMIT 1 [["id", 1]] Completed 500 Internal Server Error in 86ms (ActiveRecord: 1.3ms)

ActionView::MissingTemplate (Missing template players/new, application/new with {:locale=>[:en], :formats=>[:html, :xml], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in: * "/home/adam/workspace/tc/app/views" * "/home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.4.1/app/views" ):

Started GET "/tournaments/1/players/new" for 127.0.0.1 at 2015-11-11 17:37:01 +0100 Processing by PlayersController#new as HTML Parameters: {"tournament_id"=>"1"} Tournament Load (0.1ms) SELECT "tournaments".* FROM "tournaments" WHERE "tournaments"."id" = ? LIMIT 1 [["id", 1]] Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.1ms)

ActionView::MissingTemplate (Missing template players/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in: * "/home/adam/workspace/tc/app/views" * "/home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.4.1/app/views" ):

Player create button, rendered in action tournaments#index: (probably problem starts here)

%td= link_to 'Join', new_tournament_player_path(tournament), class: "btn btn-primary btn-md"

How can I resolve this problem?

1
It's looking for the new view file in the create action?j-dexx
Yeah something is off here - Rails would by convention look for /players/create.html.erb unless your controller redirects or renders explicitly (like render :nothing => true). Check the server logs if your create action is even being called. It might help if you include the exact steps you performed and your routes.rbmax
@japed I updated my question.Nekron

1 Answers

3
votes

As you can see from the server log your create action is not even being called here:

Started GET "/tournaments/1/players/new" for 127.0.0.1
...
Processing by PlayersController#new as HTML

CRUD in Rails

http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

In Rails and REST in general the POST request method is used to create resources. A GET request should always be idempotent and not create, alter or destroy resources on the server.

Why? Becuase GET requests are stored in the browser history. Hitting the back button will cause a record to be inadvertently created altered or destroyed! Not good.

If you want to create a resource in rails without having a separate new action or view you would use a form:

<%= form_for [tournament, Player.new] do |f| %>
  <%= f.submit %>
<% end %>

Submitting the form will issue a request to:

POST /tournaments/:tournament_id/players

Rails offers a convenience method called button_to which will create a form for you so that you have a link or button that sends the extended list of HTTP verbs (POST, PUT/PATCH, DELETE).

<%= button_to "Join", tournament_players_path(tournament), method: :post %>