List > partial > edit

HI, i have a simple list of users, underneath each one i want to display
the corresponding edit view via a partial.
BUt, it always displays the person no1 in the edit view…

Person1
…edit(partial)

Person1(should be 1)
Person2
Preson1(should be 1)
Person3
Person1(should be 1)
Person4

whats wrong here? im not passing locals to the partial…

thx

On 14 Aug 2008, at 15:05, Tom T. wrote:

Preson1(should be 1)
Person3
Person1(should be 1)
Person4

whats wrong here? im not passing locals to the partial…

What are you doing?

Fred

Frederick C. wrote:

On 14 Aug 2008, at 15:05, Tom T. wrote:

Preson1(should be 1)
Person3
Person1(should be 1)
Person4

whats wrong here? im not passing locals to the partial…

What are you doing?

Fred

woops, myu mistake: should be:

Person3

Edit Person3
Person4
Edit Person4

but the edit view is always Person1

user/_edit:
<% form_for(@user) do |f| %>
<%= f.error_messages %>
Title

<%= f.text_field :username %>

<%= f.submit "Update" %>

<% end %> ----------------------------------------- user/index

Listing Users

<% for u in @users %> <% end %>
Firstname Lastname Username
<%=h u.firstname + u.id.to_s %> <%=h u.lastname %> <%=h u.username %> <%= link_to 'Show', u %>
<%= render :partial =>'edit' %>

gives me the result above…
thx 4 looking over

On 14 Aug 2008, at 15:23, Tom T. wrote:

whats wrong here? im not passing locals to the partial…

What are you doing?

Fred

Can’t guess what’s wrong if we can’t see what you’re doing.

Fred

Frederick C. wrote:

On 14 Aug 2008, at 15:45, Tom T. wrote:

<% end %>

Well, that’s expected - in your partial you always edit @user.
if you do render :partial => ‘edit’, :object => u
and then change the partial to be
form_for(object) …

then you should be ok

Fred

ii changed it into
a)
, :object => u
and
<% form_for(u) do |f| %>
b)
, :object => u
and
<% form_for(u) do |f| %>

still the same result

Hey Tom,
in your index, you have to pass the right variable to the partial,
like Fred wrote:
render :partial => ‘edit’, :user => u

u is the local variable you are passing (you called: for u in @users)
user is then the name of the variable you will use in your edit
partial (in Fred’s example, it was called ‘object’).

So in your partial, you need to do:
<% form_for user do %>

You can call it anything you want, but the symbol you use must match
the variable name you use in your partial. In your example, you’re
using ‘u’ but you never passed a variable called ‘u’ to the partial
(you passed ‘object’).

On 14 Aug 2008, at 16:51, Tom T. wrote:

form_for(object) …
b)
, :object => u
and
<% form_for(u) do |f| %>

that should be form_for object. The current object is always available
under two names: the name derived from the partial and object.

Fred

On 14 Aug 2008, at 15:45, Tom T. wrote:

<% end %>

Well, that’s expected - in your partial you always edit @user.
if you do render :partial => ‘edit’, :object => u
and then change the partial to be
form_for(object) …

then you should be ok

Fred

deegee wrote:

Hey Tom,
in your index, you have to pass the right variable to the partial,
like Fred wrote:
render :partial => ‘edit’, :user => u

u is the local variable you are passing (you called: for u in @users)
user is then the name of the variable you will use in your edit
partial (in Fred’s example, it was called ‘object’).

So in your partial, you need to do:
<% form_for user do %>

You can call it anything you want, but the symbol you use must match
the variable name you use in your partial. In your example, you’re
using ‘u’ but you never passed a variable called ‘u’ to the partial
(you passed ‘object’).

mhh, somehow im totally confused now. the index action /view iterates
through @users with an iterator called u:

Listing Users

<% for u in @users %> <% end %>
Firstname Lastname Username
<%=h u.firstname + u.id.to_s %> <%=h u.lastname %> <%=h u.username %> <%= link_to 'Show', u %>
<%= render :partial =>'edit' , :@user => @users %>

/////////////////////////// user edit controller: def edit @user = User.find(params[:id]) end means i have somehow to use that variable.

////////////////////////////////
partial:

Editing User

<% form_for(@user) do |f| %>

Title
<%= f.text_field :username %>

<%= f.submit "Update" %>

<% end %>

…summary: means i could replace

<%= render :partial =>‘edit’ , :@user => @users %>
with
<%= render :partial =>‘edit’ , :@whatever => u %> //u because of the
iterator

and use @whatever in the edit-display:
% form_for(whatever) do |f|
???
but that cant be true, where is the @users being used from the EDIT
action out of the controller?
i cant see thriough right now. help

thx

On 14 Aug 2008, at 17:50, Tom T. wrote:

Um, that’s just wrong. :object is an option to render. if you want to
pass arbitrality named local variables use the locals option

Listing Users

<%=h u.username %> def edit <%= f.text_field :username %>

<%= f.submit "Update" %>

<% end %>

Are you reusing this same partial both from the edit action and from
the index action ?
If so then in the edit action you would do
render :partial => ‘edit’, :object => @user
in the list action
render :partial => ‘edit’, :object => u

and in the partial itself:
form_for object

Fred

First of all, it shouldn’t be :@whatever but :whatever. You want a
symbol for the name of the variable you’ll be using in the partial.

Second, it’s your index action in the controller that is calling the
index view (and also the edit partial). You’re using the @users
instance variable set by the index action, iterating through each of
them (creating a variable u) and then calling the partial view,
passing the variable ‘whatever’ if you use the symbol :whatever.

You edit action in the controller is not needed anymore, unless you
want to call it in another context. It requires a view edit (that is
not your partial), which of course could call the same partial using
render :partial => edit, :whatever => @user
see, here you’re using the @user from the edit action, because that’s
the action that called the view.

Hope that helps.

Dirk.

Sorry, my mistake, it should be :object => @user in the edit view,
and :object => u in the index view. I was thinking of locals indeed.
Would work as well :slight_smile:

deegee wrote:

Sorry, my mistake, it should be :object => @user in the edit view,
and :object => u in the index view. I was thinking of locals indeed.
Would work as well :slight_smile:

thx guys - ill try to get this done.
please stay subscribed.thx tom

ok i tried a few things, and it seems that everything depends on two
lines:

<%= render :partial =>'edit', :object=>@users %>

and
…"_edit"…
<% form_for :user, @users do |f| %>

Title
<%= f.text_field :username %> ....

facts:
yes i copied the origin edit to _edit because there a minor differences.
here i would like to use the partial “_edit”. i can see and understand
that the edit action out of the user controller wont be triggered,
though i havent read the api for that yet.
so, so far i understood what u tried to xplain is: regardless what i
pass into the partial, i have to reuse that name because it appears
within the “scope” od that function / loop (one output).
i dont understand why i should pass the list-object called @object
because that contains for my understanding all users returned by the
list-action-controller. anyhow i tried to pass on the local variable u
and neither that worked because it couldnt find the attributes later
like here: “f.text_field :username”. the few times the server doesnt
throw an exception is when the result list gives me always the same name
in the edit partial…
does anyone have a working example? or can reproduce it?
thx

Here are the two variants from the rdoc of actionview, I was thinking
of the first one but forgot to add the :locals part, sorry about that.

Renders the same partial with a local variable.

render :partial => “person”, :locals => { :name => “david” }

Renders the partial, making @new_person available through

the local variable ‘person’

render :partial => “person”, :object => @new_person

On Aug 14, 6:51 pm, Tom T. [email protected] wrote:

<%= f.text_field :username %>

I’ll try again. What you need to do to reuse the partial is decouple
it from whatever instance variables are used. That means that @ signs
are banished from your partial. use the local variable the partial
creates for you (object or the name of the partial).
when you render a partial, you can specify what that object should be
with the :object option. That’s all there is to it.

You’ll never need :object => @users.

Fred

Frederick C. wrote:

On Aug 14, 6:51�pm, Tom T. [email protected] wrote:

� � <%= f.text_field :username %>

I’ll try again. What you need to do to reuse the partial is decouple
it from whatever instance variables are used. That means that @ signs
are banished from your partial. use the local variable the partial
creates for you (object or the name of the partial).
when you render a partial, you can specify what that object should be
with the :object option. That’s all there is to it.

You’ll never need :object => @users.

Fred

fred, im really sorry, but im missing her something. when does the
partial create a variable except the local variable f?
could u give em an example base don my last post? that would be help
ful, sas well if u could use complete different names for the variables/

thx u so much!

fred, im really sorry, but im missing her something. when does the
partial create a variable except the local variable f?

if you do render :partial => ‘something’, :object => @foo

then in the context of the partial there will be two local variables:
something and object, which will have the same value as @foo.

Fred

On Aug 14, 8:48 pm, Tom T. [email protected] wrote:

hi,

mh:

<%= render :partial =>'edit', :var=>u %>

That is not supposed to work. You have to say :object => u. :object is
the name of an option, not something you can make up your self.
Similarly what you get in your partial will always be the name of the
partial and object.

Fred