Seong Kim Seong Kim - 7 months ago 14
Ruby Question

Why am I getting error "undefined method `id' for nil:NilClass" even though there's a id in Rails?

Hi I am making Rails mailer action.

routes.rb:

resources :textbook_giveaways do
member do
get 'contact', to: 'textbook_giveaways#new_contact', as: 'new_contact'
post 'contact', to: 'textbook_giveaways#send_contact', as: 'send_contact'
end
end


new_contact_html.erb:

<div class="container">
<div class="row">
<h2>ID is: <%= @textbook_giveaway.id %></h2>
<form role="form" action="" method="post" >
<!--<div class="col-sm-6">-->
<!-- <div class="col-sm-offset-4 col-sm-4 col-sm-offset-4"> -->
<div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">
<div class="form-group">
<!--<label for="InputMessage">Message</label>-->
<div class="input-group">
<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>

<label><h4>I would like to buy <strong><%=@texbook_giveaway.title%></strong></h4></label>


This line
<h2>ID is: <%= @textbook_giveaway.id %></h2>
working fine. It shows the id number.

But
<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
this gives me error.

picture
above is error picture

when I run rake routes I get:

new_contact_textbook_giveaway GET /textbook_giveaways/:id/contact(.:format) textbook_giveaways#new_contact
send_contact_textbook_giveaway POST /textbook_giveaways/:id/contact(.:format) textbook_giveaways#send_contact
textbook_giveaways GET /textbook_giveaways(.:format) textbook_giveaways#index
POST /textbook_giveaways(.:format) textbook_giveaways#create
new_textbook_giveaway GET /textbook_giveaways/new(.:format) textbook_giveaways#new
edit_textbook_giveaway GET /textbook_giveaways/:id/edit(.:format) textbook_giveaways#edit
textbook_giveaway GET /textbook_giveaways/:id(.:format) textbook_giveaways#show
PATCH /textbook_giveaways/:id(.:format) textbook_giveaways#update
PUT /textbook_giveaways/:id(.:format) textbook_giveaways#update
DELETE /textbook_giveaways/:id(.:format) textbook_giveaways#destroy


[1]: http://i.stack.imgur.com/MbiUz.png


my controller is this:

class TextbookGiveawaysController < ApplicationController
before_action :set_textbook_giveaway, only: [:show, :edit, :update, :destroy, :new_contact, :send_contact]

# GET /textbook_giveaways
# GET /textbook_giveaways.json
def index
@textbook_giveaways = TextbookGiveaway.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
end

# GET /textbook_giveaways/1
# GET /textbook_giveaways/1.json
def show
end

# GET /textbook_giveaways/new
def new
@textbook_giveaway = TextbookGiveaway.new
end

# GET /textbook_giveaways/1/edit
def edit
if not @textbook_giveaway.user_email == current_user.email || current_user.email == "codeinflash@gmail.com"
redirect_to @textbook_giveaway
end
end

# POST /textbook_giveaways
# POST /textbook_giveaways.json
def create
@textbook_giveaway = TextbookGiveaway.new(textbook_giveaway_params)
@textbook_giveaway.user_email = current_user.email
@textbook_giveaway.giveaway = true

respond_to do |format|
if @textbook_giveaway.save
format.html { redirect_to @textbook_giveaway }
flash[:success] = "Textbook giveaway was successfully created."
else
format.html { render :new }
format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /textbook_giveaways/1
# PATCH/PUT /textbook_giveaways/1.json
def update
respond_to do |format|
if @textbook_giveaway.update(textbook_giveaway_params)
format.html { redirect_to @textbook_giveaway }
flash[:success] = 'Textbook giveaway was successfully updated.'
else
format.html { render :edit }
format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
end
end
end

# DELETE /textbook_giveaways/1
# DELETE /textbook_giveaways/1.json
def destroy
@textbook_giveaway.destroy
respond_to do |format|
format.html { redirect_to textbook_giveaways_url }
format.json { head :no_content }
flash[:alert] = "Textbook giveaway was successfully destroyed."
end
end

#here for contact
def new_contact
end

def send_contact
message = params[:message]
if Contact.send_contact(@textbook_giveaway, current_user, message).deliver
flash[:success] = "Email sent."
redirect_to @textbook_giveaway
else
flash[:alert] = "There was a problem sending the email."
render :new_contact
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_textbook_giveaway
@textbook_giveaway = TextbookGiveaway.friendly.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def textbook_giveaway_params
params.require(:textbook_giveaway).permit(:title, :subject, :user_email, :description, :giveaway, :slug, :thumbnail)
#params.fetch(:textbook_giveaway, {})
end
end

Answer

It's a small typo in your code.

This line is correct:

<h2>ID is: <%= @textbook_giveaway.id %></h2>

In the line below, you misspell variable name. @texbook_giveaway should be @textbook_giveaway.

<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>

Calling unassigned instance variables in Ruby doesn't raise error. It just returns nil.