You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails:打印Wishlist的show方法HTTP路径并渲染至独立视图

问题描述

我有一个名为wishlists的简单应用,应用逻辑如下:

  • 用户进入首页
  • 用户可选择新建心愿单或查看已有心愿单
  • 用户点击新建心愿单后,进入独立视图输入心愿单名称并提交

目前新建心愿单后会跳转到对应的show视图,我希望改为打印show控制器方法的HTTP路径(/wishlists/:id(.:format))并渲染到独立视图,后续用于前端弹窗开发。

现有代码

控制器代码(WishlistsController)

class WishlistsController < ApplicationController
  before_action :set_wishlist, only: %i[ show edit update destroy ]

  
  # GET /home
  def home 
  end 
  
  # GET /wishlists
  def index
    @wishlists = Wishlist.all
  end

  # GET /wishlists/1
  def show
  end

  # GET /wishlists/new
  def new
    @wishlist = Wishlist.new
  end

  # GET /wishlists/1/edit
  def edit
  end

  # POST /wishlists
  def create
    @wishlist = Wishlist.new(wishlist_params)

    if @wishlist.save
       redirect_to @wishlist, notice: "Wishlist was successfully created."
    else
      render :new, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /wishlists/1
  def update
    if @wishlist.update(wishlist_params)
      redirect_to @wishlist, notice: "Wishlist was successfully updated."
    else
      render :edit, status: :unprocessable_entity
    end
  end

  # DELETE /wishlists/1
  def destroy
    @wishlist.destroy
    redirect_to wishlists_url, notice: "Wishlist was successfully destroyed."
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_wishlist
      @wishlist = Wishlist.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def wishlist_params
      params.require(:wishlist).permit(:name, :wishlist_link)
    end
end

表单视图(_form.html.erb)

<%= form_with(model: wishlist) do |form| %>
  <% if wishlist.errors.any? %>
    <div style="color: red">
      <h2><%= pluralize(wishlist.errors.count, "error") %> prohibited this wishlist from being saved:</h2>

      <ul>
        <% wishlist.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div>
    <%= form.label :name, style: "display: block" %>
    <%= form.text_field :name %>
  </div>

  <div>
    <%= form.submit %>
  </div>
<% end %>

新建视图(new.html.erb)

<h1>New wishlist</h1>

<%= render "form", wishlist: @wishlist %>

<br>


<div>
  <%= link_to "Back to homepage", root_path %>
</div>
解决方案

实现步骤:

  1. 修改控制器create方法
    替换原有的跳转逻辑,改为渲染自定义视图并传递路径变量:

    # POST /wishlists
    def create
      @wishlist = Wishlist.new(wishlist_params)
    
      if @wishlist.save
        # 生成带实际ID的show路径
        @show_path = wishlist_path(@wishlist)
        # 渲染自定义展示视图
        render :show_path_display, notice: "Wishlist was successfully created."
      else
        render :new, status: :unprocessable_entity
      end
    end
    
  2. 创建展示路径的视图文件
    在app/views/wishlists目录下新建show_path_display.html.erb,内容如下:

    <h1>心愿单创建成功</h1>
    <p>Show路径:<%= @show_path %></p>
    
    <div>
      <%= link_to "返回首页", root_path %>
      <%= link_to "查看心愿单", @show_path %>
    </div>
    
  3. 如果需要显示带占位符的路由模板
    若要输出/wishlists/:id(.:format)这种原始路由格式,修改create方法中的变量:

    @show_route_template = Rails.application.routes.named_routes[:wishlist].path.spec.to_s
    

    然后在视图中替换为<%= @show_route_template %>即可。


内容的提问来源于stack exchange,提问作者Aly Dabbous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 03:55:40