Jose Hales-garcia
2006-Jul-19 20:20 UTC
[Rails] How do I change :type attribute in a view?
I''m using single table inheritance and want to change the :type attribute from an admin view. How do I do this given that :type has to be handled with special care? -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jul-20 01:38 UTC
[Rails] Re: How do I change :type attribute in a view?
Jose Hales-garcia wrote:> I''m using single table inheritance and want to change the :type > attribute from an admin view. How do I do this given that :type has to > be handled with special care?type is protected from mass assignment so you have to do something like: @model = Model.find(params[:id]) @model[:type] = params[:model][:type] @model.update_attributes(params[:model]) -- We develop, watch us RoR, in numbers too big to ignore.
Jose Hales-Garcia
2006-Jul-21 03:22 UTC
[Rails] Re: How do I change :type attribute in a view?
Mark Reginald James wrote:> Jose Hales-garcia wrote: >> I''m using single table inheritance and want to change the :type >> attribute from an admin view. How do I do this given that :type has to >> be handled with special care? > > type is protected from mass assignment so you have to do something like: > > @model = Model.find(params[:id]) > @model[:type] = params[:model][:type] > @model.update_attributes(params[:model])Thank you on the advice for the controller code. But I''m getting an error in the view (see below). Are there special techniques for handling the :type attribute inside of a view? wrong argument type String (expected Module) Extracted source (around line #12): 9: <td></td> 10: </tr> 11: <tr> 12: <td><%= select :document, :type, ApplicationHelper::DOCUMENT_TYPES %></td> 13: <td></td> 14: </tr> 15: <tr> -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jul-21 05:57 UTC
[Rails] Re: How do I change :type attribute in a view?
Jose Hales-Garcia wrote:> Thank you on the advice for the controller code. But I''m getting an > error in the view (see below). Are there special techniques for > handling the :type attribute inside of a view? > > wrong argument type String (expected Module) > Extracted source (around line #12): > > 12: <td><%= select :document, :type, ApplicationHelper::DOCUMENT_TYPES %></td>Yes, it''ll be using the Ruby built-in type method rather than the AR type attribute method because the latter uses method_missing. You''ll have to do one of three things: 1. Change to a different attribute name using set_inheritance_column 2. Add a method to the document model so that type can be accessed under a different method name 3. Code as: select_tag ''document[type]'', options_for_select(ApplicationHelper::DOCUMENT_TYPES, @document[:type]) -- We develop, watch us RoR, in numbers too big to ignore.