# POST Requests are mapped to my index action

**URL:** <https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698>\
**Category:** Rails\
**Created:** [August 24, 2008, 10:28am UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698 "2008-08-24T10:28:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 10:28am UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/1 "2008-08-24T10:28:21Z")

</div>

Hi all !  
I’ve got a problem in my application. I’ve written a bug\_reports  
controller. It is pretty RESTful, there are index, new and create  
actions. Everytime I submit a new bug\_report, the index action is  
called, not the create action, you can see the log here:  
[http://pastie.org/258857](http://pastie.org/258857).  
My form looks like this:

\<% form\_for @bug\_report, :url =\> bug\_reports\_path do |f| -%\> 
 \<%= f.text\_field :title, :value =\> "Title" %\> 

 \<%= f.text\_area :body %\> 

 \<%= f.submit "Submit", :disable\_with =\> 'Submiting...' %\> 
\<% end -%\>

My routes.rb contains this line: map.resources :bug\_reports

And finally, my BugReportsController looks like this:  
class BugReportsController \< ApplicationController  
login\_required  
def index  
@bug\_reports = BugReport.all  
end

def new  
@bug\_report = BugReport.new  
end

def create  
@bug\_report = BugReport.new params[:bug\_report]  
if @bug\_report.save  
flash[:notice] = “Thank you for submitting this bug.”  
else  
flash[:warning] = “Your bug report was not saved. Please try  
again.”  
end  
redirect\_to root\_url  
end  
end

This error doesn’t occur in other parts of my app. I really don’t know  
why this happens, I hope you can help me. Thank you very much in  
advance,  
Christoph

PS: I’ve written a small patch regarding  
options\_from\_collection\_for\_select. You can find it here:  
[#890 in options\_from\_collection\_for\_select: text/value\_method are chainable - Ruby on Rails - rails](http://rails.lighthouseapp.com/projects/8994/tickets/890-in-options_from_collection_for_select-text-value_method-are-chainable).  
I would be happy if someone would give me feedback on this.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/2 "2008-08-24T12:10:11Z")

</div>

On Sun, Aug 24, 2008 at 1:27 AM, Christoph [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Try using the following:

\<% form\_for(@bug\_report) do |f| -%\>

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/3 "2008-08-24T12:28:24Z")

</div>

No, this doesn’t help. The form seems to be correct regardless if I  
specify the url or not:

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/4 "2008-08-24T12:53:34Z")

</div>

Hi, I was able to get it working here without any issues. I built the  
application from scratch. Here’s the new.html.erb:

# New bug\_report

\<% form\_for(@bug\_report) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label :body %\>  
 \<%= f.text\_area :body %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, bug\_reports\_path %\>

-Conrad

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/5 "2008-08-24T13:33:58Z")

</div>

Hi, try the following:

# POST /bug\_reports

# POST /bug\_reports.xml

def create  
@bug\_report = BugReport.new(params[:bug\_report])

```
respond_to do |format|
  if @bug_report.save
    flash[:notice] = 'BugReport was successfully created.'
    format.html { redirect_to(@bug_report) }
  else
    format.html { render :action => "new" }
  end
end

```

end

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/6 "2008-08-24T13:18:43Z")

</div>

Don’t you think the error is somewhere in the code handling the  
request. The form is generated correct, and it also sends the request  
correct to the server, with all parameters, as you can see in the log  
here: [http://pastie.org/258857](http://pastie.org/258857)

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 25, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/7 "2008-08-25T17:50:27Z")

</div>

Sometimes my router freaks out until I restart the server. It will  
default to :controller/:action, which has the effect of displaying  
“index”, even though I POST-ed (which should route it to “create”).

When stuff goes nutty, I always restart the server.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 24, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/8 "2008-08-24T13:42:10Z")

</div>

On Sun, Aug 24, 2008 at 4:18 AM, Christoph [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Don’t you think the error is somewhere in the code handling the  
> request. The form is generated correct, and it also sends the request  
> correct to the server, with all parameters, as you can see in the log  
> here: [http://pastie.org/258857](http://pastie.org/258857)

I don’t think that the request is correct because it should look like  
the  
following:

Processing BugReportsController#create (for 127.0.0.1 at 2008-08-24  
04:29:26) [POST]  
Session ID:  
BAh7BzoMY3NyZl9pZCIlZDNlNzUzMTBiYTg4OGM5NjI2MWNlNmJmNDdmOWIz  
MjciCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh  
c2h7AAY6CkB1c2VkewA=–38e907fd59880233fd4f597c3f76448f80efa3b1  
Parameters: {“bug\_report”=\>{“title”=\>“This is a test.”, “body”=\>“This  
is a  
test.”}, “commit”=\>“Create”,  
“authenticity\_token”=\>“69184209cf20041cc992c5f33367e7e328932e42”,  
“action”=\>“create”, “controller”=\>“bug\_reports”}  
BugReport Create (0.000863) INSERT INTO “bug\_reports” (“created\_at”,  
“title”, “body”, “updated\_at”) VALUES(‘2008-08-24 11:29:26’, ‘This is a  
test.’, ‘This is a test.’, ‘2008-08-24 11:29:26’)  
Redirected to [http://localhost:3000/bug\_reports/0](http://localhost:3000/bug_reports/0)  
Completed in 0.01773 (56 reqs/sec) | DB: 0.00086 (4%) | 302 Found [  
[http://localhost/bug\_reports](http://localhost/bug_reports)]

Your code is performing a GET instead of a POST. Thus, you’re hitting  
the  
index action instead of the create action.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [August 28, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/post-requests-are-mapped-to-my-index-action/146698/9 "2008-08-28T13:42:13Z")

</div>

Thank you, I don’t know why but routing is correct now. Restarting the  
server seemed to work.
