Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:00:36+00:00 2026-06-09T03:00:36+00:00

I have a simple model for suggestions in my Rails 3 application. I am

  • 0

I have a simple model for suggestions in my Rails 3 application. I am trying to add a simple link_to link on the index view that when clicked marks the suggestion as approved.

So far I have the following code:

routes.rb

resources :suggestions do
 get :approve
end

suggestions_controller.rb

def approve
 @suggestion = Suggestion.find(params[:id])
 @suggestion.update_attribute(:approved, 'true')
 @suggestion.update_attribute(:approved_on, Time.now)
 redirect_to suggestion_path
end

suggestion.rb

attr_accessible :author, :details, :localip, :title, :approved, :approved_on

schema.rb

create_table "suggestions", :force => true do |t|
 t.string   "title"
 t.string   "author"
 t.text     "details"
 t.string   "localip"
 t.datetime "created_at",                     :null => false
 t.datetime "updated_at",                     :null => false
 t.boolean  "approved",    :default => false
 t.datetime "approved_on"
end

index.html.erb (suggestions)

<% if @suggestion.approved? %>
    <%= @suggestion.approved_on %>
<% else %>
    Not yet approved. (<%= link_to "Approve", approve_suggestion_url(@approve), :method => :put %>)
<% end %>

When using the above code, I get the following exception error:

undefined method `approved?' for nil:NilClass

Am I missing a step out somewhere something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:00:37+00:00Added an answer on June 9, 2026 at 3:00 am

    Most probably, the code you have posted is inside an each loop like

    <% @suggestions.each do |suggestion| %>
      ...
      <% if @suggestion.approved? %>
        <%= @suggestion.approved_on %>
      <% else %>
        Not yet approved. (<%= link_to "Approve", approve_suggestion_url(@approve), :method => :put %>)
      <% end %>
    <% end %>
    

    You need to use suggestion variable instead of @suggestion instance variable. Also, to construct suggestion approve url, you’ll need to use suggestion instead of @approve. Code should be like

      <% if suggestion.approved? %>
        <%= suggestion.approved_on %>
      <% else %>
        Not yet approved. (<%= link_to "Approve", approve_suggestion_url(suggestion), :method => :put %>)
      <% end %>
    

    Also, you should modify your routes.rb to reflect that approve action is a member action.

    resources :suggestions do
      put :approve, on: :member
    end
    

    To understand the difference between member and collection routes, read difference between collection route and member route in ruby on rails?.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model forms, that is being rendered through the following jinja2
I have been building a Rails application that performs accounting functionality. As part of
I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
I have a simple model class (Part), which pulls from it's information from a
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have a simple model containing a FileField among others. When I use a
I have a simple model from simulink and I would like to generate code
Suppose I have a simple model, such as Record: @Model public class Record {
I have a simple User model which is associated to many Town objects using
I have a simple ActiveRecord model called Student with 100 records in the table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.