# Passing Values to Views

**URL:** <https://www.ruby-forum.com/t/passing-values-to-views/174179>\
**Category:** Rails\
**Created:** [August 21, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179 "2009-08-21T20:48:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 21, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/1 "2009-08-21T20:48:16Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 21, 2009, 9:18pm UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/2 "2009-08-21T21:18:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 21, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/3 "2009-08-21T23:41:31Z")

</div>

> 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! 🙂 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
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 21, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/4 "2009-08-21T21:06:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 22, 2009, 12:28am UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/5 "2009-08-22T00:28:20Z")

</div>

2009/8/21 doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 22, 2009, 4:20am UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/6 "2009-08-22T04:20:43Z")

</div>

> 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
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [August 22, 2009, 6:03am UTC](https://www.ruby-forum.com/t/passing-values-to-views/174179/7 "2009-08-22T06:03:38Z")

</div>

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](http://www.classifiedscript.in). See if it helps you.
