# How can I access the value of params\[:tags\] in my validate f

**URL:** <https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693>\
**Category:** Rails\
**Created:** [February 13, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693 "2006-02-13T19:34:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 13, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/1 "2006-02-13T19:34:45Z")

</div>

I would like to have tags mandatory in my app.

The following keeps giving me error that params is nill.

def validate()  
if (@params[:tags])  
errors.add(@params[:tags], ":tags must be entered ")  
end  
end

Why cannot I access params in my model class?  
The field for tags is called “tags”

How else can I add an error using errors.add?

I finally got to use flash[:notice] in my controller but I’m sure  
there is a better way to do this.

Many thanks to the member of this great list for their assistance.

Frank

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 13, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/2 "2006-02-13T21:41:14Z")

</div>

On Feb 13, 2006, at 10:34 AM, softwareengineer 99 wrote:

> Why cannot I access params in my model class?
> 
> Yahoo! Mail  
> Use Photomail to share photos without annoying attachments.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Frank-

```
You cannot access params in your model because it breaks MVC. Your

```

model should not know anything about params or session at all. You  
will have to refactor the method to pass in the params to a method on  
your model from your controller. Or rethink the problem you are  
trying to solve.

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 15, 2006, 12:39am UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/3 "2006-02-15T00:39:46Z")

</div>

I’m trying to pass the name of a column to update along with it’s new  
value. Given these as my params:

{  
“id”=\>“3”,  
“field”=\>“asset\_type”,  
“selected”=\>“item8”,  
“controller”=\>“inventory”,  
“action”=\>“update”  
}

asset\_type is a column in my database, and the following works:

def update  
@asset = Asset.find(params[:id])  
@asset.asset\_type = params[:selected]  
@asset.save  
@asset.reload  
render\_text @asset.asset\_type  
end

Is there a way to replace the two instances of @asset.asset\_type with  
something along the lines of @asset.params[:field] (or @asset.send  
(params[:field]) )? It’s the syntax that’s killing me…

- Peter

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 15, 2006, 12:51am UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/4 "2006-02-15T00:51:52Z")

</div>

On Tue, 2006-02-14 at 15:36 -0800, Peter T Bosse II wrote:

> Is there a way to replace the two instances of @asset.asset\_type with  
> something along the lines of @asset.params[:field] (or @asset.send  
> (params[:field]) )? It’s the syntax that’s killing me…

* * *

id=\>3 means a single row in your table  
selected=\>‘item8’ is probably confusing because it isn’t a column. You  
should probably identify it as ‘asset\_type’ and forget the extra work of  
‘field=\>’ as that doesn’t clarify anything…only confuses it. Check the  
‘link\_to’ statement where this all comes from and fix it there.

Craig

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 15, 2006, 1:00am UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/5 "2006-02-15T01:00:59Z")

</div>

On Feb 14, 2006, at 3:48 PM, Craig W. wrote:

> > }
> 
> Check the  
> ‘link\_to’ statement where this all comes from and fix it there.

* * *

Thanks for the response!

I guess I should have been more clear as to what I’m trying to do. I  
have an Ajax function that would like to repurpose for use on all of  
my pop-up fields (I’m not using link\_to). In this particular  
situation, I know I’m trying to update the column “asset\_type”, but  
I’d like to re-use this code to be able to update any arbitrary  
column (which is named in :field) with the results of “:selected”.  
Is this possible?

- Peter

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 15, 2006, 1:46am UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/6 "2006-02-15T01:46:16Z")

</div>

> I guess I should have been more clear as to what I’m trying to do. I  
> have an Ajax function that would like to repurpose for use on all of  
> my pop-up fields (I’m not using link\_to). In this particular  
> situation, I know I’m trying to update the column “asset\_type”, but  
> I’d like to re-use this code to be able to update any arbitrary  
> column (which is named in :field) with the results of “:selected”.  
> Is this possible?
> 
> - Peter

Peter,

Haven’t tested this exactly, only on a dummy class, but I’m pretty  
certain it will work with little modification if any:

```
def update
  @asset = Asset.find(params[:id])
  @assent.send("#{params[:field]}=".to_sym, params[:selected])
  @asset.save
  @asset.reload
  render_text @asset.send(params[:field])
end

```

Joe N.

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 15, 2006, 1:49am UTC](https://www.ruby-forum.com/t/how-can-i-access-the-value-of-params-tags-in-my-validate-f/53693/7 "2006-02-15T01:49:18Z")

</div>

> Peter,  
> end
> 
> Joe N.

* * *

Perfect! Thank you so much!

- Peter
