1

I have a simple web app built on rails called wishlist. The logic of the app is as follows:

  1. User Enters the homepage
  2. User can either start a new wishlist || view his wishlists
  3. When the user clicks on start a new wishlist the user is brought to a separate view where he enters the name of the wishlist & submits the name.
  4. After the wishlist is created, he is brought to another view which includes a call-to-action called 'DONE' & the entire URL of the website - the URL includes the #show path of the wishlist that has just been created.
  5. When the user clicks on 'DONE', he is brought to the index view of all the wishlists where he can view each & every wishlist as well as click on a specific wishlist & view it separately in the #show view.

My problem is that the entire URL of the website gets rendered normally in step No. 4 - when the user has just created a wishlist. But when the user views a specific wishlist via the #show path, the entire HTTP path of the URL doesn't get rendered normally & the only thing that is returned is the #show path (even though I'm using the same lines of code in the both places).

Here is a video of my app to exemplify what I mean: https://www.loom.com/share/f6f9a64b875c4a5ea69e65c1b1cdc0bc

And this where I'm rendering the entire HTTP path of my website sucessfully:

<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 %>

And this where I'm unable to render the entire HTTP path of my website (even though I'm using the exact link_to method):

<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>

This is my wishlist controller if it helps:

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

1 Answer 1

0

When @wishlist is saved you're setting @show_url and then using it as a link text:

<% @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>

In the show action there is no @show_url set. Because it's an instance variable, it defaults to nil:

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

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

If name is nil, link_to helper will use path/url as a name.


To get a full url:

<%= wishlist_url(@wishlist) %>

<%= polymorphic_url(@wishlist) %>
<%= url_for([@wishlist, only_path: false]) %>
<%= full_url_for(@wishlist) %>
<%= link_to nil, [@wishlist, only_path: false] %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.