# Passing information to the controller from view

**URL:** <https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138>\
**Category:** Rails\
**Created:** [January 7, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138 "2009-01-07T18:33:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 7, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/1 "2009-01-07T18:33:39Z")

</div>

Hi everyone,

I have a template called createFile.html.erb which has some ruby code in  
it. The ruby code pulls information from the database because i’m  
looping through some records. Is it possible to send all of the  
information in the createFile.html.erb output to the createFile method  
in the controller? I’m wanting to save the content of it all to a text  
file…

Or is there an easier way to produce the same outcome?

Thanks,  
McKenzie.

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 7, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/2 "2009-01-07T18:41:36Z")

</div>

On 7 Jan 2009, at 17:33, Ryan M. wrote:

> 

render\_to\_string ?

Fred

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 8, 2009, 12:45am UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/3 "2009-01-08T00:45:15Z")

</div>

Frederick C. wrote:

> On 7 Jan 2009, at 17:33, Ryan M. wrote:
> 
> > 
> 
> render\_to\_string ?
> 
> Fred

render\_to\_string puts the whole html document in the data including the  
template, however I only need part of the file. This code might explain  
it a little better.

#demo\_controller.rb

1. def createFile
2. 

```
render_to_string :layout => false

```

3. 

```
@demo = Demo.find(params[:id])

```

4. 
5. 

```
data = render_to_string

```

6. 
7. 

```
f = File.open("#{RAILS_ROOT}/public/demo/demotest.txt", "wb")

```

8. 

```
f.write(data)

```

9. 

```
f.close

```

10. 
11. end

#demo/createFile.html.erb

1. ModelTemp {
2. 

```
    Name "Mod"

```

3. Halt 0.0
4. DType “Define”
5. FData {
6. 

```
Events {

```

7. 

```
<% for event in display_demo_events(@demo) %>

```

8. 

```
  Event {

```

9. 

```
                Name "<%= event.name -%>"

```

10. 

```
                Desc "<%= event.description -%>"

```

11. 

```
                Constant {FRate="<%= event.f_rate -%>"}

```

12. 

```
              }

```

13. 

```
  <% end %>

```

14. 

```
}

```

15. 

```
Outputs {

```

16. 

```
  <% for deviation in display_demo_events(@demo) %>

```

17. 

```
  Deviation {

```

18. 

```
                Name "<%= deviation.output_class -%>"

```

19. 

```
                "<%= deviation.description -%>"

```

20. 

```
              }

```

21. 

```
  <% end %>

```

22. 

```
}

```

23. }
24. }

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 8, 2009, 1:22am UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/4 "2009-01-08T01:22:51Z")

</div>

On Jan 7, 2009, at 6:45 PM, Ryan M. wrote:

> the  
> template, however I only need part of the file. This code might  
> explain  
> it a little better.
> 
> #demo\_controller.rb
> 
> 1. def createFile
> 2. 
> 
> ```
> render_to_string :layout => false
> 
> ```

render here…

> 1. 
> 
> ```
> @demo = Demo.find(params[:id])
> 
> ```
> 
> 2. 
> 3. 
> 
> ```
> data = render_to_string
> 
> ```

…and here??? (without giving :layout =\> false)

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 8, 2009, 2:11am UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/5 "2009-01-08T02:11:23Z")

</div>

In your controller, you just need

data = render\_to\_string({:action =\> ‘create.html.erb’,  
:layout=\>false})

I’m doing a PDF (using HTMLDOC) that way. Works awesome.

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 8, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/6 "2009-01-08T15:47:43Z")

</div>

Brian H. wrote:

> In your controller, you just need
> 
> data = render\_to\_string({:action =\> ‘create.html.erb’,  
> :layout=\>false})
> 
> I’m doing a PDF (using HTMLDOC) that way. Works awesome.

Hi Brian, that works a treat thanks. I’m also trying to output some  
information to a pdf. Have you got an email address I can contact you  
at?

Thanks

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [January 8, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/passing-information-to-the-controller-from-view/157138/7 "2009-01-08T19:52:46Z")

</div>

bphogan at gmail dot com

On Thu, Jan 8, 2009 at 8:47 AM, Ryan M.
