Bitwise
Bitwise

Reputation: 8461

Weird behavior when trying to render show page - Rails

Currently I have an app that does all the basic CRUD operations. The latest problem is I'm trying to add a link the all the people in my subscribers table that will render the show page for each individual subscriber. Everything seems straight forward but when I try to render the show page I get the object id instead of the instance of the person I'm trying to see. I'm not exactly sure what I'm doing wrong but I'll post code and screen shots for clarity.

Controller:

    class SubscribersController < ApplicationController
  helper_method :sort_column, :sort_direction

  def index
    @search = Subscriber.search(params[:q])
    @subscriber = @search.result
    @search.build_condition if @search.conditions.empty?
    @search.build_sort if @search.sorts.empty?
  end

  def show
    @subscriber = Subscriber.find_by(id: params[:id])
  end

  def new
    @subscriber = Subscriber.new
  end

  def create
    @subscriber = Subscriber.new(subscriber_params)
    if @subscriber.save
      @subscriber.touch(:subscription_date)
      SubscriberMailer.welcome_subscriber(@subscriber).deliver_now
      flash[:notice] = "Subscriber Has Been Successfully Created"
      redirect_to new_subscriber_path(:subscriber)
    else
      render "new"
    end
  end

VIEW:

  <h1><%= @subscriber %></h1>

Just trying to confirm that I'm getting the right info before building out.

ERROR:

enter image description here

as you can see the show action is rendering this object. I'm trying to render the subscriber first_name

HTML:

<% @subscriber.each do |subscriber| %>
    <tr>
      <td><%= link_to subscriber.first_name, subscriber_path(subscriber) %></td>
      <td><%= subscriber.last_name %></td>
      <td><%= subscriber.email %></td>
      <td><%= subscriber.phone_number %></td>
      <td><%= subscriber.date_joined %></td>
      <td><%= subscriber.expiration_date %></td>
      <td><%= subscriber.visit %></td>
      <td><%= subscriber.days_till_expired %></td>
      <td><%= subscriber.mug_number %></td>
    </tr>
  <% end %>

As you can see I have a link_to and the route I'm trying to pass the the first_name attribute.

I hope this is enough info let me know if you need more?

Upvotes: 0

Views: 32

Answers (2)

dtakeshta
dtakeshta

Reputation: 214

In your loop you are passing the entire subscriber object to the URL.

update your link_to to this:

link_to subscriber.first_name, subscriber_path(subscriber.id)

That should fix your problem.

Also, if you haven't installed the pry gem, I suggest you do. This will allow you to test variables in the console that are set up in your model or controller.

Upvotes: 1

Ren
Ren

Reputation: 1374

If i understand correctly you want to show the subscriber name in the h1 tag. If you want the first name then you need <h1><%= @subscriber.first_name %></h1>

Upvotes: 0

Related Questions