ruby-on-rails 呈现我的ruby on rails web应用程序的整个http路径

hk8txs48  于 2023-03-24  发布在  Ruby
关注(0)|答案(1)|浏览(135)

我有一个简单的基于Rails的Web应用程序,名为wishlist。该应用程序的逻辑如下:
1.用户进入主页
1.用户可以开始新的愿望清单||查看他的愿望清单
1.当用户点击开始新的愿望清单时,用户被带到一个单独的视图,在该视图中他输入愿望清单的名称并提交该名称。
1.在愿望列表被创建之后,他被带到另一个视图,该视图包括被称为“完成”的动作调用&网站的整个URL-该URL包括刚刚被创建的愿望列表的#show路径。
1.当用户点击“DONE”时,他被带到所有愿望清单的索引视图,在那里他可以查看每个愿望清单以及点击特定的愿望清单并在#show视图中单独查看。
我的问题是,在第4步中,当用户刚刚创建了一个愿望清单时,网站的整个URL会正常呈现。但是当用户通过#show路径查看特定的愿望清单时,URL的整个HTTP路径不会正常呈现&唯一返回的是#show路径(尽管我在这两个地方使用了相同的代码行)。
以下是我的应用程序的视频,以举例说明我的意思:https://www.loom.com/share/f6f9a64b875c4a5ea69e65c1b1cdc0bc
这是我成功渲染我的网站的整个HTTP路径的地方:

<h1>Wishlist created!</h1>

<p>The URL for your wishlist is:</p>

<p><%= link_to @show_url, wishlist_path(@wishlist) %></p> 

<p>Use this URL to view your wishlist.</p>

<%= link_to "Done", wishlists_path %>

在这里,我无法呈现我的网站的整个HTTP路径(即使我使用确切的link_to方法):

<p style="color: green"><%= notice %></p>

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

<br>

<p><%= link_to @show_url, wishlist_path(@wishlist) %></p> 
  
<div>
  <%= button_to "Delete this wishlist", @wishlist, method: :delete %>
  <%= link_to "Back to wishlists", wishlists_path %>
</div>

这是我的愿望清单控制器,如果它有帮助:

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
    @wishlist = Wishlist.find(params[:id])
  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
      @show_url = url_for(controller: 'wishlists', action: 'show', id: @wishlist.id, only_path: false)
      render :show_url
    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
kmbjn2e3

kmbjn2e31#

保存@wishlist时,您正在设置@show_url,然后将其用作链接文本:

<% @show_url = url_for(controller: 'wishlists', action: 'show', id: @wishlist.id, only_path: false) %>

# this is a full url, which you can see, it's the name of the link.
#           vvvvvvvvv
<%= link_to @show_url, wishlist_path(@wishlist) %>
#                      ^^^^^^^^^^^^^^^^^^^^^^^^ 
# this is always only a path `/wishlists/1`, `href` of the link

<a href="/wishlists/1">
  http://localhost:3000/wishlists/1
</a>

show动作中没有设置@show_url。因为它是一个示例变量,所以默认为nil

<%= link_to nil, wishlist_path(@wishlist) %>

<a href="/wishlists/1">
  /wishlists/1
</a>

如果name为nil,link_to helper将使用path/url作为名称。
获取完整的url:

<%= wishlist_url(@wishlist) %>

<%= polymorphic_url(@wishlist) %>
<%= url_for([@wishlist, only_path: false]) %>
<%= full_url_for(@wishlist) %>
<%= link_to nil, [@wishlist, only_path: false] %>

相关问题