# Passing params to a component

**URL:** <https://www.ruby-forum.com/t/passing-params-to-a-component/82329>\
**Category:** Rails\
**Created:** [March 15, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329 "2006-03-15T19:23:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![afg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@afg](https://www.ruby-forum.com/u/afg)\
**Post date:** [March 15, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329/1 "2006-03-15T19:23:45Z")

</div>

I see from  
[http://rubyonrails.org/api/classes/ActionController/Components.html](http://rubyonrails.org/api/classes/ActionController/Components.html)

that is possible to pass paramaters to a component.

I don’t understand the exact syntax.

I’m trying:  
@master\_id = @params[‘master\_id’]  
in the components controller but it is not working.

==== Details:  
In my calling view I have:

\<%= h(@opportunity.id) %\>

  
\<%= render\_component :controller =\> “todos”, :params =\> {  
“master\_id” =\> @opportunity.id } %\>  

The above displays @opportunity.id so I know it is a valid variable.

The components (todos) controller has:  
def list  
@master\_id = @params[‘master\_id’]  
@todos = Todo.find :all  
render :layout =\> false  
end

Which is my attempt to instantiate and load @master\_id.

The todos list view has a simple debug display of:

\<%= h(@master\_id) %\>

Nothing is being displayed by the above.

–  
Greg F.  
The Norcross Group  
Forensics for the 21st Century

---

<div class="post-metadata">

**Author:** ![afg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@afg](https://www.ruby-forum.com/u/afg)\
**Post date:** [March 16, 2006, 12:46am UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329/2 "2006-03-16T00:46:32Z")

</div>

Resolved.

I switched to using a session variable.

ie. session[:case\_id] = params[:case\_id] etc.

It is working for me now and even better only the detail records  
associated with my master are showing up. That took a little more  
code, but not bad at all.

Greg

On 3/15/06, Greg F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ==== Details:  
> The components (todos) controller has:  
> \<%= h(@master\_id) %\>
> 
> Nothing is being displayed by the above.
> 
> –  
> Greg F.  
> The Norcross Group  
> Forensics for the 21st Century

–  
Greg F.  
The Norcross Group  
Forensics for the 21st Century

---

<div class="post-metadata">

**Author:** ![afg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@afg](https://www.ruby-forum.com/u/afg)\
**Post date:** [March 16, 2006, 1:01am UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329/3 "2006-03-16T01:01:47Z")

</div>

If I call render\_component from within a view, what would be the  
appropriate way to have that call pass along any necessary data to  
the controller that needs it? :params =\> {“var” =\>  
@needed\_instance\_var} ??

Michael J.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![afg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@afg](https://www.ruby-forum.com/u/afg)\
**Post date:** [March 16, 2006, 1:26am UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329/4 "2006-03-16T01:26:06Z")

</div>

Michael J. wrote:

> If I call render\_component from within a view, what would be the  
> appropriate way to have that call pass along any necessary data to the  
> controller that needs it? :params =\> {“var” =\> @needed\_instance\_var} ??

Yes.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![afg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@afg](https://www.ruby-forum.com/u/afg)\
**Post date:** [March 16, 2006, 2:26am UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329/5 "2006-03-16T02:26:43Z")

</div>

If your component happens to be generated from the ajax\_generator then  
I found that I needed to catch the param ASAP in the controller and  
save it out as a session variable.

Then the list / new /create methods could retrieve it from the session  
variable.

ie. session[:var] = params[:var] if I recall the syntax correctly

Greg

On 3/15/06, Michael J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 15, 2006, at 3:46 PM, Mark Reginald J. wrote:
> 
> > > The components (todos) controller has:  
> > > If you want to call the component’s “list” method rather than
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Greg F.  
The Norcross Group  
Forensics for the 21st Century

---

<div class="post-metadata">

**Author:** ![afg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@afg](https://www.ruby-forum.com/u/afg)\
**Post date:** [March 16, 2006, 12:49am UTC](https://www.ruby-forum.com/t/passing-params-to-a-component/82329/6 "2006-03-16T00:49:34Z")

</div>

Greg F. wrote:

> The components (todos) controller has:  
> \<%= h(@master\_id) %\>
> 
> Nothing is being displayed by the above.

If you want to call the component’s “list” method rather than  
“index” you have to add an “:action =\> ‘list’” option to your  
render\_component call.

–  
We develop, watch us RoR, in numbers too big to ignore.
