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


#1

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


#2

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

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
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#3

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

#4

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


#5

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

#6

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.


#7

Peter,
end

Joe N.


Perfect! Thank you so much!

  • Peter