ChaosKnight Posted April 11, 2010 Share Posted April 11, 2010 Hi all, I hope there are some RoR users on this forums, I have a script that is supposed to update some country info on the site that I'm busy with, here is my controller code: def edit @country = Country.find_by_country_id(params[:name]) end def update @country = Country.find_by_country_id(params[:name]) puts @country if @country.update_attributes(params[:country]) flash[:notice] = 'The country was saved successfully.' redirect_to :action=>'index' else flash[:notice] = 'The country could not be saved.' redirect_to :action=>'edit' end end And here is my view code: <% form_for([:admin, @country]) do |f| %> <%= f.error_messages %> <div> <%= f.label :country_id %><br /> <%= f.text_field :country_id, :class=>"form_text_field" %> </div> <div> <%= f.label :country %><br /> <%= f.text_field :country, :class=>"form_text_field" %> </div> <div> <%= f.label :description %><br /> <%= f.text_area :description, :rows=>6, :cols=>45, :class=>"form_text_area" %> </div> <div> <%= f.label :area %><br /> <%= f.text_field :area, :class=>"form_text_field" %> </div> <div> <%= f.label :provinces %><br /> <%= f.text_field :provinces, :class=>"form_text_field" %> </div> <div> <%= f.label :cities %><br /> <%= f.text_area :cities, :rows=>6, :cols=>45, :class=>"form_text_area" %> </div> <div> <%= f.label :towns %><br /> <%= f.text_area :towns, :rows=>6, :cols=>45, :class=>"form_text_area" %> </div> <div> <%= f.label :capital %><br /> <%= f.text_field :capital, :class=>"form_text_field" %> </div> <div> <%= f.label :currency %><br /> <%= f.text_field :currency, :class=>"form_text_field" %> </div> <div> <%= f.label :cultural %><br /> <%= f.text_area :cultural, :rows=>6, :cols=>45, :class=>"form_text_area" %> </div> <div> <%= f.label :languages %><br /> <%= f.text_field :languages, :class=>"form_text_field" %> </div> <div> <%= f.label :time %><br /> <%= f.text_field :time, :class=>"form_text_field" %> </div> <div> <%= f.label :dial %><br /> <%= f.text_field :dial, :class=>"form_text_field" %> </div> <div> <%= f.label :voltage %><br /> <%= f.text_field :voltage, :class=>"form_text_field" %> </div> <div> <%= f.label :car %><br /> <%= f.text_field :car, :class=>"form_text_field" %> </div> <div> <%= f.label :parks%><br /> <%= f.text_area :parks, :rows=>6, :cols=>45, :class=>"form_text_area" %> </div> <div> <%= f.label :active %><br /> <%= f.text_field :active, :class=>"form_text_field" %> </div> <div> <%= f.submit 'Update', :class=>"form_button" %> </div> <% end %> I got banned from a Google Group (Ruby on Rails: Talk), just for asking a question, it's ridiculous, but I hope someone here will be able to help. The error is a NoMethodError, and it says "You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.update_attributes"... I worked an entire day just to try and fix this error, but I still couldn't find a solution. Thank you in advance, I really appreciate it Sorry, only after I have posted I saw the "Other programming language" Section, if one the moderators can please transfer it, I would be very grateful Quote Link to comment https://forums.phpfreaks.com/topic/198232-ruby-on-rails-edit-form/ Share on other sites More sharing options...
ChaosKnight Posted April 11, 2010 Author Share Posted April 11, 2010 Haha I finally fixed it!! I looked at the address bar and saw that it was passing the wrong values, and the error message was right - I didn't expect it haha I only change the @country = Country.find(params[:id]) I forgot that I made use of RESTful routes, which by default pass the :id parameter... Topic Solved !! Quote Link to comment https://forums.phpfreaks.com/topic/198232-ruby-on-rails-edit-form/#findComment-1040100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.