Mike Norton
Mike Norton

Reputation: 71

Links with Ruby on Rails

If I have the following code in my controller:

# encoding: utf-8
module Admin

  class SylabusController < BaseController


  def show_all
      @questions = @topic.questions.all
    end

And I have the index where I would like to "call" the show_all in order that appears a new web page with all the questions. How does be the link?

 <%= link_to 'All the questions'.html_safe, @sylabus.show_all, class: 'btn' %>

With the following error.

NoMethodError in Admin/mupets#index

Showing app/views/admin/sylabus/index.html.erb where line #41 raised:

undefined method `show_all' for nil:NilClass

Is it my error in the link code? or Do I have to define something in the routes?

Thank you for your time and help

Upvotes: 0

Views: 72

Answers (2)

just so
just so

Reputation: 1138

In your SylabusController:

def index
   # are you sure that @topic is not null?
   @questions = @topic.questions
end

In your view

 <%= link_to 'All the questions', @questions, class: 'btn' %>

Just get class variable associateded with controller action :)

Upvotes: 0

user229044
user229044

Reputation: 239311

You cannot link directly to actions on controllers, you can only make requests which are connected to a controller/action via your routing table.

You need a route which will reach that action, and then you need a view which will render output to the user.

Upvotes: 1

Related Questions