重定向到特定名称的帖子从用户输入在Ruby on Rails?



我是RoR的新手,因此很抱歉愚蠢的问题:(我有一个游戏模型,代码为字符串。有一个欢迎/索引视图在我的应用程序与一个简单的form_to输入。我希望用户重定向到一个游戏与特定的代码后,他提交的形式。我明白我应该在中组合一个方法和redirect_to在Welcome_controller,但我就是不知道如何…

Welcome_controller.rb:

class WelcomeController < ApplicationController
def index
end
def redirect
redirect_to ?game with a code that equals :param from input?
end
end

欢迎/指数:

<h1>Let's join the game!</h1>
<%= form_tag redirect_path do %>
<%= text_field_tag(:param) %>  
<%= submit_tag("Search") %>
<% end %>

routes.rb:

Rails.application.routes.draw do
get 'welcome/index'
resources :games
get 'games/index'
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

game.rb:

class Game < ApplicationRecord
validates :name, :presence => true
end

games_controller:

PREFACE      = ('A'..'Z').to_a << ?_
SUFFIX       = ('0'..'9').to_a
PREFACE_SIZE = 2
SUFFIX_SIZE  = 3
class GamesController < ApplicationController
before_action :set_game, only: %i[ show edit update destroy ]
# GET /games or /games.json
def index
@games = Game.all
end
# GET /games/1 or /games/1.json
def show
end
# GET /games/new
def new
@game = Game.new
@game.code = gen_name
end
def gen_name
PREFACE.sample(PREFACE_SIZE).join << SUFFIX.sample(SUFFIX_SIZE).join
end
# GET /games/1/edit
def edit
end
# POST /games or /games.json
def create
@game = Game.new(game_params)
respond_to do |format|
if @game.save
format.html { redirect_to game_url(@game), notice: "Game was successfully created." }
format.json { render :show, status: :created, location: @game }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /games/1 or /games/1.json
def update
respond_to do |format|
if @game.update(game_params)
format.html { redirect_to game_url(@game), notice: "Game was successfully updated." }
format.json { render :show, status: :ok, location: @game }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
# DELETE /games/1 or /games/1.json
def destroy
@game.destroy
respond_to do |format|
format.html { redirect_to games_url, notice: "Game was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_game
@game = Game.find(params[:id])
end
# Only allow a list of trusted parameters through.
def game_params
params.require(:game).permit(:code, :name)
end
end

config/routes.rb中,您已经定义了resources :games,它为CRUD操作创建默认路径。对于您试图在这里获得的show操作,它将导致/games/:id和辅助方法将是game_path。你也可以通过在app目录下运行rails routes -c games命令来检查。它应该返回games_controller

的所有路径在GamesController#show动作的before_action回调中,你正在使用Game.find(params[:id])找到一个游戏对象。:id参数是你需要传递给我之前提到的路径帮助器的参数,所以特定游戏的路径应该是game_path(id: game.id)。然后将自动转换为参数。或者,你可以将游戏对象传递给路径助手,它会为你做这样的工作:game_path(game)

现在在WelcomeController#redirect操作中,您可以从表单提交中获得参数形式的游戏代码。你需要先像这样找到提交代码的游戏:

game = Game.find_by(code: params[:param])

如果每个游戏的代码都是唯一的,这应该是可行的。现在你有了正确的游戏记录,你所需要做的就是重定向到我之前提到的路径:

redirect_to game_path(game)

最新更新