Jump to content

Ruby on Rails - Edit Form


ChaosKnight

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/198232-ruby-on-rails-edit-form/
Share on other sites

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  ;D

 

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 !!  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.