# Weird generate path

**URL:** <https://www.ruby-forum.com/t/weird-generate-path/188080>\
**Category:** Rails\
**Created:** [May 5, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/weird-generate-path/188080 "2010-05-05T14:03:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [May 5, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/weird-generate-path/188080/1 "2010-05-05T14:03:27Z")

</div>

Hi,  
I have a simple form in my controller “before\_home”. In the form I want  
to create a user, si I use the users\_path on my form.  
But look at the html generated… Why do I have an action before\_home? I  
should be “users” in the action.  
Does someone knows where this could come from?

View:  
\<% form\_for users\_path do |f| %\>

```
<p>
  <%= f.label :email %>
  <%= f.text_field :email %>
  <%= f.label :ville %>
  <%= text_field_with_auto_complete :user, :ville_name, { :size =>

```

15 }, { :url =\> formatted\_villes\_path(:js), :method =\> :get, :param\_name  
=\> ‘search’ } %\>

```
  <%= f.submit "Suivant" %>
</p>

```

\<%end%\>

Generated HTML:

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [May 5, 2010, 10:52pm UTC](https://www.ruby-forum.com/t/weird-generate-path/188080/2 "2010-05-05T22:52:44Z")

</div>

Floyd Joseph wrote:

> Hi,
> 
> form\_for expects the first parameter to be a new user object and if  
> that isn’t supplied then the form action is set to the action that  
> rendered the page; this is why you see action="/before\_home". The  
> reason user\_path isn’t working is because it doesn’t reference a user  
> object. If you wish to have the form submitted to /user then set up  
> the user object and specify the url. See below…
> 
> To create a user I suggest you create a new user object and store it  
> in an instance variable:
> 
> @user = User.new
> 
> Then in the form\_for method you can use that value:
> 
> \<% form\_for :user, :url =\> { :controller =\> users\_path } do |f| %\>
> 
> Hope this helps

Thanks for your help Floyd, it works now.  
However, I have another issue. How do I display the errors regarding  
that my form is in a different controller.

```
if @user.save
  flash[:notice] = "Successfully created user."
  redirect_to root_path
else
  render :action => 'index', :controller => "before_home"
end
```

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [May 5, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/weird-generate-path/188080/3 "2010-05-05T17:41:36Z")

</div>

Hi,

form\_for expects the first parameter to be a new user object and if  
that isn’t supplied then the form action is set to the action that  
rendered the page; this is why you see action="/before\_home". The  
reason user\_path isn’t working is because it doesn’t reference a user  
object. If you wish to have the form submitted to /user then set up  
the user object and specify the url. See below…

To create a user I suggest you create a new user object and store it  
in an instance variable:

@user = User.new

Then in the form\_for method you can use that value:

\<% form\_for :user, :url =\> { :controller =\> users\_path } do |f| %\>

Hope this helps

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [May 5, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/weird-generate-path/188080/4 "2010-05-05T23:45:12Z")

</div>

In response to your second question, you would not be able to  
redisplay the form with errors by using render because the action is  
in another controller. However you can render the template that  
created the form:

if @user.save  
flash[:notice] = “Successfully created user.”  
redirect\_to root\_path  
else  
render :template =\> “before\_home/new” # I am assuming new was the  
action in before\_home  
end

This will redisplay the form along with the errors.

However, you should consider your application design, it would be  
easier to control the creating and updating of records under the  
controller that is related to a specific model. Consider implementing  
a more RESTful application, contact for more info or if you have  
further problems.
