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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F68e4842b0a5487678e26046fdb2fdf3?d=identicon&s=25 softwareengineer 99 (Guest)
on 2006-02-13 19:34
(Received via mailing list)
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
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-13 21:41
(Received via mailing list)
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
> Rails@lists.rubyonrails.org
> 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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
46a97bfed2cc82d33f7aedcb5dbc66a5?d=identicon&s=25 Peter T Bosse II (Guest)
on 2006-02-15 00:39
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-15 00:51
(Received via mailing list)
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
46a97bfed2cc82d33f7aedcb5dbc66a5?d=identicon&s=25 Peter T Bosse II (Guest)
on 2006-02-15 01:00
(Received via mailing list)
On Feb 14, 2006, at 3:48 PM, Craig White 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
032fdb4cd4a3c65ebb77846dfa724679?d=identicon&s=25 Joe Noon (Guest)
on 2006-02-15 01:46
(Received via mailing list)
> 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 Noon
46a97bfed2cc82d33f7aedcb5dbc66a5?d=identicon&s=25 Peter T Bosse II (Guest)
on 2006-02-15 01:49
(Received via mailing list)
> Peter,
>     end
>
> Joe Noon

----

Perfect!  Thank you so much!

- Peter
This topic is locked and can not be replied to.