Passing Values to Views

I have an @member object (e.g., @member=Member.find(5)) with which I
am doing the basic CRUD stuff. Records can be modified by either the
administrative user or by other users in their “profile” settings.
The two views are very similar but not identical. In an attempt to
keep my code DRY, I am trying to use a single view which gets shaped
into the proper form by a couple of parameters appended to the URL.
Without validation it all works perfectly.

Here’s the problem. With validation added, I need to render the view
in order to have error messages displayed. However, I need to use
redirect_to in order to pass the needed parameters in the URL. In
googling the issue, one commentator suggested saving the @member
object in either a flash or session variable. The idea sounded good;
but, it didn’t work. Virtually everything else that I read on google
seemed to say that one needs to use render in order to access the
error messages.

Under these circumstances, I’m concerned that I might be straying from
the beaten path by trying to pass values to the view in the URL. I’m
thinking that maybe Rails doesn’t want me to be doing that. Maybe
Rails wants me to somehow include those values in the @member object.

Anyway, my question is this: How should I be approaching this issue?

Thanks for any input.

          ... doug

If the fields common to both types of users goes into a partial, then
the regular user’s view just renders that common partial. You can have
the admin view show the admin-changeable fields, and include the common
partial, giving the admin access to the generic user fields.

Two controllers, one for users, one for the admin. And to be paranoid,
you can have the user controller scrub the params of fields that a
regular user shouldn’t be modifying before attempting the save.

The model doing the validations should be doing the validations, so to
speak. Shouldn’t matter who edited the data, the rules are the rules.

And after failing a validation or save, you should just (re)render the
edit action, not redirect, as bill pointed out.

Where are you redirecting to? Above you say ‘the two views’ which, to
me, implies two controllers. Yes? No? Explain, please.

No. In referring to “two views” I meant the two views that I would
have had had I used a two-view approach. I elected to use a single
view because the differences were quite small.

If you redirect you’ll lose the errors.

Yep. I’m painfully aware of that fact! :slight_smile: I’m not sure that
articulated my question very well. I think what I was expressing more
than anything else was surprise that I couldn’t or shouldn’t pass the
errors in a redirect. To me it seemed that that would be a very
common thing that a lot of people would want to do. So, it was
setting off bells in my head that I was probably doing something out
of the mainstream.

Between your comments and those of Ar, it appears that where I made my
mistake was in trying to consolidate those views. I think that I
understand what needs to be done now. Thanks to the both of you for
your help.

    ... doug

Hi Doug,

On Fri, 2009-08-21 at 11:22 -0700, doug wrote:

I have an @member object (e.g., @member=Member.find(5)) with which I
am doing the basic CRUD stuff. Records can be modified by either the
administrative user or by other users in their “profile” settings.
The two views are very similar but not identical. In an attempt to
keep my code DRY, I am trying to use a single view which gets shaped
into the proper form by a couple of parameters appended to the URL.

Not sure what you mean by ‘a single view’. In general, we tend to use
partials to refactor out duplication in views.

Without validation it all works perfectly.

Here’s the problem. With validation added, I need to render the view
in order to have error messages displayed. However, I need to use
redirect_to in order to pass the needed parameters in the URL.

Where are you redirecting to? Above you say ‘the two views’ which, to
me, implies two controllers. Yes? No? Explain, please.

In googling the issue, one commentator suggested saving the @member
object in either a flash or session variable. The idea sounded good;
but, it didn’t work. Virtually everything else that I read on google
seemed to say that one needs to use render in order to access the
error messages.

If you redirect you’ll lose the errors. A redirect creates an entirely
new request / response cycle and @errors is an instance variable. You
could save it in the session, but I wouldn’t. It’s cleaner just to have
two views, one for your admin controller and one for the other, that use
a common partial for DRY purposes, and let Rails do it’s thing. But
then, I may be misunderstanding what you’re trying to do entirely.

Anyway, my question is this: How should I be approaching this issue?

HTH,
Bill

2009/8/21 doug [email protected]:

in order to have error messages displayed. Â However, I need to use
Rails wants me to somehow include those values in the @member object.
I suspect that you are making the whole thing more complex than it
needs be, as what you are saying does not make sense to me. Is your
problem solved just by checking the profile of the current user in the
view erb file and showing the appropriate bits of the view based on
that?

Colin

I suspect that you are making the whole thing more complex than it
needs be, as what you are saying does not make sense to me.

Colin, I couldn’t agree with you more. This whole thing was botched
from the beginning with my original post. The subject doesn’t even
relate to the issue.

Please allow me to re-post a new and hopefully more clear post with an
appropriate subject. However, I do sincerely appreciate your response
and those of the other responders.

Thanks.

     ... doug

Doug J. wrote:

I suspect that you are making the whole thing more complex than it
needs be, as what you are saying does not make sense to me.

Colin, I couldn’t agree with you more. This whole thing was botched
from the beginning with my original post. The subject doesn’t even
relate to the issue.

Please allow me to re-post a new and hopefully more clear post with an
appropriate subject. However, I do sincerely appreciate your response
and those of the other responders.

Thanks.

     ... doug

I am not sure if this would help, but still you can try. I have a
working application that you can download from
http://www.classifiedscript.in. See if it helps you.