I''m trying to grab a temporary variable from my view and then pass along that value to my model My view code is this: <td width="40" align="center"><%= text_field ''product'', ''qty_added'', {:size=>3} And I''m trying to pass the qty_added variable to my model. item.quantity = qty_added It doesn''t seem to be working.. I''m sure I''m missing many fundamental things but if somebody could start me down the correct path.. Thx.. -- Posted via http://www.ruby-forum.com/.
Vince: There is probably some clever way of doing what you ask. For myself, controllers are the middlemen, so my views talk to my controllers and my controllers talk to my model. Does that help? bruce On 20-Jan-06, at 4:21 PM, Vince W. wrote:> I''m trying to grab a temporary variable from my view and then pass > along > that value to my model > > My view code is this: > <td width="40" align="center"><%= text_field ''product'', ''qty_added'', > {:size=>3} > > > And I''m trying to pass the qty_added variable to my model. > item.quantity = qty_added > > > It doesn''t seem to be working.. I''m sure I''m missing many fundamental > things but if somebody could start me down the correct path.. > > Thx.. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> There is probably some clever way of doing what you ask. For myself, > controllers are the middlemen, so my views talk to my controllers and > my controllers talk to my model. Does that help? >Hi Bruce, I''m very new to the whole programming thing so yes it does help. I''m still kind of lost though.. In this line, <td width="40" align="center"><%= text_field ''product'', ''qty_added'', {:size=>3} I don''t even seem to be correctly capturing any data. I have a display_cart.rhtml file where I print the value of that variable using <pre><%= @qty_added.inspect %></pre> But that always comes up as ''nil'' I figure once I get the qty_added stored correctly I can then work on the controller. Does that sound right? -- Posted via http://www.ruby-forum.com/.
One piece of added information to the mix. I think the big issue is that where I submit the data I am not passing the qty_added. Here is that relevant code: <%= link_to ''Add to Cart'', {:action => ''add_to_cart'', :id => product }, :class => ''addtocart'' %><br/> I''ve defined the form like this: <td width="40" align="center"><%= text_field ''product'', ''qty_added'', {:size=>3} What should I modify in the ''Add to Cart'' link to also capture the quantity? -- Posted via http://www.ruby-forum.com/.
This code implies that you are using a GET method to submit your data. Even inside a form, a link_to will create an <a href ... > link. Look at your logs (log/development.log) and see what the params hash has in it when the action is invoked (you are running tail -f log/development.log, right? :). If I understand the original problem, you have a "meta-field" called qty_added that is not a database column. Its behavior is to exist solely to add to the quantity column that is in the database. Rails won''t automatically know what to do when that unknown field comes back in the params hash, so you will have to manually extract it and stuff it in the model. E.g. # controller code def addtocart thingie = Thing.find(params[:id]) thingie.quantity += params[:product][''qty_added''] # assuming you wanted to add... begin thingie.save! rescue # take some corrective action. end end Alternatively, you can define the behavior in your model by adding accessors for qty_added and making the write accessor add to quantity. Does this help? Does anyone else have thoughts on this? Vince W. wrote:> One piece of added information to the mix. I think the big issue is > that where I submit the data I am not passing the qty_added. Here is > that relevant code: > > <%= link_to ''Add to Cart'', > {:action => ''add_to_cart'', :id => product }, > :class => ''addtocart'' %><br/> > > > I''ve defined the form like this: <td width="40" align="center"><%= > text_field ''product'', ''qty_added'', {:size=>3} > > What should I modify in the ''Add to Cart'' link to also capture the > quantity?-- Posted via http://www.ruby-forum.com/.
Steve Ross wrote:> This code implies that you are using a GET method to submit your data. > Even inside a form, a link_to will create an <a href ... > link. Look at > your logs (log/development.log) and see what the params hash has in it > when the action is invoked (you are running tail -f log/development.log, > right? :).I suppose it is a get. I tried using a form instead just to see if it would help, but I''m having trouble accessing variables from database in the form. For instance, in _form.rhtml I have code that says: <td class="producttitle"> <div class="titlename"><%= h(product.title) %></div> </td> But that simple code gives me an "undefined local variable or method `product'' for #<#<Class:0x4081f164>:0x4081f0b0>" error> > If I understand the original problem, you have a "meta-field" called > qty_added that is not a database column.That''s correct. In my original model I had a line setting quantity manually. i.e. item.quantity = 1. I want to change that to item.quantity = qty_added but can''t seem to pass the variable to my controller.> Its behavior is to exist solely > to add to the quantity column that is in the database. Rails won''t > automatically know what to do when that unknown field comes back in the > params hash, so you will have to manually extract it and stuff it in the > model. E.g. > > # controller code > def addtocart > thingie = Thing.find(params[:id]) > thingie.quantity += params[:product][''qty_added''] # assuming you > wanted to add... > begin > thingie.save! > rescue > # take some corrective action. > end > end >I tried the above but got an undefined method quantity error. I then tried adding attr_accessor to my models for both product.rb and line_item.rb but that didn''t solve it. Couldn''t really figure out where to define quantity to make it happy.> Alternatively, you can define the behavior in your model by adding > accessors for qty_added and making the write accessor add to quantity. >I would be happy to try this too.. really I''m just looking for the easiest solution. :ol> Does this help? Does anyone else have thoughts on this?I''m starting to understand things better.. I appreciate your help.. please keep throwing suggestions at me!!! -Vince -- Posted via http://www.ruby-forum.com/.