Shubhi Agarwal
Shubhi Agarwal

Reputation: 21

I am having some problems in printing some data through the controller in Rails

I am new on Rails. I want that if a person enters "entity1" on the form (or some other entity which is present in a list called ENTITIES in the code below), the form should display its relationships. The task to check whether the entity is contained in the list of entities is successful, I can't understand how to print the relationships which are in has called RELATION_SHIPS. I tried:

#puts EntityList::RELATION_SHIPS[params[:user][:entity_name]] 

But it is giving me an error saying that template is missing or RELATION_SHIPS is unrecognised. How do I solve this?

#users_controller.rb

class EntityList

  ENTITIES = ['entity1','entity2','entity3']
  #entity1 = {:name => 'Entity One',:position => 1 :relationships => 'entity2', 'entity4'}

  RELATION_SHIPS = {:entity1 => "entity2", :entity2 => "entity3"}
end

class UsersController < ApplicationController
  layout 'admin'
  #require File.expand_path('././myconfig')  #=> C:/ruby/require/expand_path/ok.rb    loaded

  def new
    @user = User.new
  end

  def create
    if EntityList::ENTITIES.include?(params[:user][:entity_name])
      flash[:notice] = "The entity you entered is valid!!"

      redirect_to(:action => "helloworld")

      #puts EntityList::RELATION_SHIPS[params[:user][:entity_name]]
    else
      redirect_to(:action => "SorryPage")
    end
  end
end

Upvotes: 0

Views: 116

Answers (4)

Iain
Iain

Reputation: 4203

There's a definite problem here and a possible problem. The possible problem is that when you puts a string, that output appears in the terminal, not in the output sent to the web browser. If you want the output to appear in the web browser, you need to put it in the view somehow - either by setting an @variable in the controller, or making a helper_method in the controller and calling that from the view (the latter being my preferred option). For debugging output, though, using puts and having the output on the terminal (for rails s) or logs (for pow, passenger or similar) is fine.

The definite problem is that if you puts nil, I wouldn't expect to see any output (except a newline). The keys of your RELATION_SHIPS hash are symbols. params[:user][:entity_name] is a string. Looking up a string in a hash keyed by symbols will return nil, which - as per the observation about puts nil - is a bit boring. You probably want to make RELATION_SHIPS a hash with indifferent access, although you may want to call .to_sym on the value you're using to key into it.

Upvotes: 0

DGM
DGM

Reputation: 26979

First of all, try using `Rails.logger.debug' instead of puts. As for the template error, it's probably coming from the next request - you say to redirect to a different action. Do you have methods and templates for 'helloworld' and 'Sorrypage' ?

As for style, this whole example is very wrong. If you don't do things in the rails style, you ask for pain.

First, I'd move the EntityList into a model, and add methods to get info rather than just exposing constants. In your example, you had redundant information between ENTITIES and RELATION_SHIPS (Why the underscore?). A fundamental philosophy in rails is "Don't Repeat Yourself". You can DRY up those constants by using just a hash and using methods on the hash:

class EntityList

  ENTITIES = {:entity1 => "entity2", :entity2 => "entity3"}

  def self.valid_entity(e)
    ENTITIES.has_key?(e)
  end

  def self.entity_name(e)
    ENTITIES[e]
  end

end

and then use that in the controller:

  def create
    if EntityList.valid_entity(params[:user][:entity_name])
      flash[:notice] = "The entity you entered is valid!!"
      redirect_to(:action => "hello_world")
    else
      redirect_to(:action => "sorry_page")
    end
  end

See how short and sweet this makes the controller? Notice I changed the actions - they should be at least lower case, underscore, to match rails conventions, although even better would be if they went to REST style actions.

Be sure that your routes file is set up to accept them, and that you have methods for those actions, and templates.

Finally, the parameters indicates this is part of a form with a user object... and by redirecting you are going to lose all that form data. You're going to have to solve that. Really, the whole ENTITY thing should be a validation in the User model, and then the create method can create a new user and save like in the standard scaffolds.

Upvotes: 1

Dougui
Dougui

Reputation: 7230

You couldn't do anything after the redirectmethod. You must to do this before...

Edit : add code

  def create
    if EntityList::ENTITIES.include?(params[:user][:entity_name])
      flash[:notice] = "The entity you entered is valid!!"
      puts EntityList::RELATION_SHIPS[params[:user][:entity_name]]
      redirect_to(:action => "helloworld")
    else
      redirect_to(:action => "SorryPage")
    end
  end

Create a app/views/users/helloworld.html.erb and show the result in the console.

Upvotes: 0

Super Engineer
Super Engineer

Reputation: 1976

As Dougui said you can't do anything after redirect method. Also if you are trying to print relationships in browser you have to print it in 'view'. 'Puts' method will print anything in command prompt.

Upvotes: 0

Related Questions