ruby-on-rails - 如何创建没有 View 的对象

标签 ruby-on-rails

我想创建玩家对象,没有该对象模型的 View ,也没有附加参数。这是为此对象创建的操作:`

def create
  @player = @tournament.players.new

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

该对象也不需要额外的参数,因为所有参数都是默认设置的。正如你所看到的,我尝试解决我的问题 “render :nothing => true”但这不起作用,这是结果:

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

更新:

路线.rb

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

服务器日志:

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" ):

玩家创建按钮,在 Action 锦标赛#index中呈现:(可能问题从这里开始)

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

如何解决这个问题?

最佳答案

正如您从服务器日志中看到的那样,您的创建操作甚至没有在这里被调用:

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

在 Rails 和 REST 中,通常使用 POST 请求方法来创建资源。 GET 请求应始终是幂等的,并且不会创建、更改或销毁服务器上的资源。

为什么?因为 GET 请求存储在浏览器历史记录中。点击后退按钮将导致记录被无意中创建、更改或销毁!不好。

如果您想在 Rails 中创建资源而无需单独的 new 操作或 View ,则可以使用表单:

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

提交表单将向以下地址发出请求:

POST /tournaments/:tournament_id/players

Rails 提供了一种名为 button_to 的便捷方法这将为您创建一个表单,以便您有一个链接或按钮来发送 HTTP 动词的扩展列表(POST、PUT/PATCH、DELETE)。

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

关于ruby-on-rails - 如何创建没有 View 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655034/

相关文章:

ruby-on-rails - 删除 rspec 中的测试 - 更改(模型,:count) failing - Why is reload needed?

javascript - jquery 未在外部 js 文件中与 Rails 一起运行

mysql - 尝试连接到 AWS MySQL 实例时 ActiveRecord NoDatabaseError

ruby-on-rails - 如何使用元编程进行 DRY?

ruby-on-rails - 编写用于在 Heroku 上部署的 Multi-Tenancy Rails 3 应用程序

ruby-on-rails - 使用 RVM 在 vi​​m 中重新生成 ctags

ruby-on-rails - ruby on rails 如何将 http 请求映射到处理函数?

ruby-on-rails - rails 响应跨域请求在本地开发,spotify 应用程序开发

javascript - 打开书签后如何将页面居中弹出?

ruby-on-rails - smalltalk 的 sinatra 是什么?