2

I have two files. .rb (with Ruby code) and .erb(HTML file with some ruby script). I am calling a Ruby function in .rb from .erb.

.erb

<a href="<%= url_for :action => :showProducts(i) %>">Click here</a> 

.rb

def showProducts(param)

//Some code

end

I am able to call a function without passing parameters to it. But as and when I pass parameters to function and then call it, I receive an error. I know this is the incorrect way to call a parametrized function from .erb. What is the correct way of calling a parameterized function from HTML?

8
  • Looks like your syntax is correct. What's not working? What errors are you getting? Commented Nov 15, 2011 at 15:10
  • It just comes as "Error loading page." Commented Nov 16, 2011 at 4:15
  • Then you'll find more details in the log. What does that say? Commented Nov 16, 2011 at 4:24
  • When I try debugging, control doesn't go inside the function. (I tried with solution below but still no success). Commented Nov 16, 2011 at 4:37
  • Yes, and as I asked in the previous question, what does the log say? Not the debugger -- the log? Commented Nov 16, 2011 at 4:39

3 Answers 3

1

If you add in another key/value pair to the hash in url_for

<%= url_for :action => :showProducts, :product => "toaster" %>

Your URL should go from, say, http://localhost:3000/showProducts to http://localhost:3000/showProducts?product=toaster

Here, we're adding parameters to the GET request. To access these parameters in the controller we use the params hash. For example to get the product (toaster):

params[:product] #=> "toaster"
Sign up to request clarification or add additional context in comments.

Comments

1

I found the solution to my problem :

<a href="<%= url_for :action => :showProducts, :id=> 'Hello' %>">  

.rb function:

def showProducts(param)

//Some code

end

Comments

0

I found the solution

def showProducts 

 @params['product']

end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.