-1

I am trying to put render code in my edit.html.erb and create _form.html.erb. I have these code for edit.html.erb

    <h1>Main#edit</h1>
    <h1>Editing zone</h1>
    <%= render partial: "form", locals: {addresses: @addresses} %>

and these code for _form.html.erb

    <%= form_for(addresses)do |f| %>
  <p>
  <b>name</b><br>
  <%= f.text_field :address %>
  </p>
  <p>
  <%= f.submit %>
  </p>
  <% end %>

and addresses is a set of address.

and I have this erro: Couldn't find Address with 'id'= Extracted source (around line #21):

  def edit
 @address = Address.find(params[:id])  this is 21th line
  if request.post?
  if (@address.update_attributes(address_params))
  redirect_to :action=>"index"

would you please help to check anything wrong in my code?

here is the code of my controller:

    class MainController < ApplicationController
    def index
    @addresses = Address.all
    end

    def new

    end

    def add
    address = Address.new(address_params)
    if (address.save)
    redirect_to :action=>"index"
    else
     flash[:notice]="Error saving, try again"
     redirect_to :action=>"index"
     end
    end

    def edit
    @address = Address.find(params[:id])
    if request.post?
    if (@address.update_attributes(address_params))
    redirect_to :action=>"index"
    else
    render :action=>"edit"
        end
      end
    end

    def delete
    address = Address.find(params[:id])
    address.destroy
    redirect_to :action=>"index"
    end

    private
    def address_params
    params.require(:address).permit(:name,:address,:email,:phone)
    end
    end

here is my model: my data structure

June
  • 17
  • 3
  • The original code is in https://github.com/siddharthkaza/addressbook_rails42.git. Sorry, it's not undefined variable error, I also like to know how the _form call edit function. I don't understand, if I need to pass id value, where I specify it? – June Feb 26 '16 at 21:46
  • I don't know how partial render works in rails. I think problem problem may either because I forget pass id value to edit function, or my address is somehow not read( I am sure addresses[1] and more are not empty). – June Feb 26 '16 at 23:45
  • (1) you should spend a **little** more time formatting your code, (2) you should post the specific stack trace. – jvillian Feb 27 '16 at 01:24
  • sorry I am puzzled by this too..I don't know the trace so I am confusing. It is a common sample. Partial render both in new and edit view, but I don't know how it work. – June Feb 27 '16 at 01:32

1 Answers1

0

I would need to see more of your controller code, but it looks like Rails is trying to find an address matching the id within params here:

 def edit
   @address = Address.find(params[:id])

It seems like you have a route setup to handle this edit action:

get 'resource/:id', to: 'ResourceController#edit'

When you try to access '/resource/1' (or whatever your url is) in your browser, it should find the address (assuming there is an address with id=1 in your database)

Bobby Matson
  • 1,448
  • 12
  • 14
  • I added more info, would you please check again? Address here is just an attribute. – June Feb 26 '16 at 23:46