softwareengineer 99
2006-Feb-13 18:34 UTC
[Rails] How can I access the value of params[:tags] in my validate function?
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 --------------------------------- Yahoo! Mail Use Photomail to share photos without annoying attachments. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060213/2974db17/attachment.html
Ezra Zygmuntowicz
2006-Feb-13 20:39 UTC
[Rails] How can I access the value of params[:tags] in my validate function?
On Feb 13, 2006, at 10:34 AM, softwareengineer 99 wrote:> 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 > > Yahoo! Mail > Use Photomail to share photos without annoying attachments. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsFrank- 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
Peter T Bosse II
2006-Feb-14 23:36 UTC
[Rails] sending column name to update along with value
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
On Tue, 2006-02-14 at 15:36 -0800, Peter T Bosse II wrote:> 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... >---- 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
Peter T Bosse II
2006-Feb-15 00:00 UTC
[Rails] sending column name to update along with value
On Feb 14, 2006, at 3:48 PM, Craig White wrote:> On Tue, 2006-02-14 at 15:36 -0800, Peter T Bosse II wrote: >> 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... >> > ---- > 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.---- 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
> 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? > > - PeterPeter, 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
Peter T Bosse II
2006-Feb-15 00:48 UTC
[Rails] sending column name to update along with value
>> 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]) > @asset.send("#{params[:field]}=".to_sym, params[:selected]) > @asset.save > @asset.reload > render_text @asset.send(params[:field]) > end > > Joe Noon---- Perfect! Thank you so much! - Peter