あなたは適切に入力を入れ子にしていない:
<%= form_for @game, html: { role: 'form' }, remote: true %>
<%= f.text_field :user_stake, class: 'form-control' %>
<%= hidden_field_tag 'user_id', current_user.id %>
<%= hidden_field_tag 'jackpot_id', @jackpot.id %>
# ..
<% end %>
これは、次のparamsハッシュを与える:
{
game: {
user_stake: 1.2
},
user_id: 3,
jackpot_id: 4
}
ホワイトリストに送信すると、次のようになります。
{
user_id: 3,
jackpot_id: 4
}
<%= form_for @game, html: { role: 'form' }, remote: true %>
<%= f.text_field :user_stake, class: 'form-control' %>
<%= f.hidden_field_tag 'user_id', current_user.id %>
<%= f.hidden_field_tag 'jackpot_id', @jackpot.id %>
# ..
<% end %>
し、適切にそれらをホワイトリスト: - NEVERはのparamsを通じて、現在のユーザーIDを渡す
private
def game_params
params.require(:game)
.permit(:user_stake, :user_id, :jackpot_id)
end
をしかし、ここで巨大な警告フラグがあるソリューションに
は、単に巣の入力であります悪意のあるユーザーがWebインスペクタだけでハックするのはとても簡単です。代わりに、セッションから直接値を使用します。
ユーザーのパスワードを除いて、またはアプリケーションの秘密を知ること以外は、偽造することはできません。
ゲームがジャックポットに属している場合は、nested resourceとして設定し、パスにIDを設定すると、親リソースに子を追加していることを明確に示すRESTful構造が作成されますリクエスト本体の重要な情報
# routes.rb
resources :jackpots do
resources :games, shallow: true
end
class GamesController
before_action :set_jackpot, only: [:new, :create, :index]
# GET /jackpots/:jackpot_id/games/new
def new
@game = @jackpot.games.new
end
# POST /jackpots/:jackpot_id/games
def create
@game = @jackpot.games.new(game_params) do |g|
g.user = current_user
end
if @game.save
# ...
else
# ...
end
end
# GET /jackpots/:jackpot_id/games
def index
@games = @jackpot.games
end
private
def set_jackpot
@jackpot = Jackpot.includes(:games)
.find(params[:jackpot_id])
end
def game_params
params.require(:game).permit(:user_stake)
end
end
<%= form_for [@jackpot, @game], remote: true, html: { role: 'Form' } do |f| %>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="input-group">
<%= f.number_field :user_stake, class: 'form-control' %>
<span class="input-group-btn">
<%= f.submit 'Go', :html => { :type => "button" }, class: "btn btn-default" %>
</span>
</div>
</div>
</div>
<% end %>
必要隠れ入力がないか注意してください。
現時点では、フォームを送信するためのレールサーバーのヒントはありますか? –
'before_action:game_params、only:[:create]'を削除します。それは無駄な時間/記憶を除いて全く何もしません。 – max
DB列タイプとは何ですか? – max