Simple redirect_to shows in log but doesn't redirect

On Aug 2, 3:36 pm, Neil B. [email protected] wrote:

i was saying you need neither -

def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
end

How then would it create the comment?

why wouldn’t it ? - that’s creating the comment on the second line.

Fred

From inside your create method in the controller, what does
request.format return?

If the request is a plain old html request is should put “text/html”
If the request is an ajax request is should put “text/javascript”
if the request puts “text/html” or anything besides “text/javascript”
you can force the format by adding: request.format = :js to the
controller method.

Joshua Mckinney wrote:

From inside your create method in the controller, what does
request.format return?

If the request is a plain old html request is should put “text/html”
If the request is an ajax request is should put “text/javascript”
if the request puts “text/html” or anything besides “text/javascript”
you can force the format by adding: request.format = :js to the
controller method.

If I try this

def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
request.format = :js
end

or this

def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
end

both give

Comment Create (0.5ms) INSERT INTO “comments” (“created_at”, “body”,
“updated_at”, “story_id”) VALUES(‘2010-08-02 16:38:36’, ‘cat’,
‘2010-08-02 16:38:36’, 2)

ActionView::MissingTemplate (Missing template comments/create.erb in
view path app/views):

Still can’t see where it’s going wrong.

In peace Neil

Have you tried naming the file create.js.rjs?

Joshua Mckinney wrote:

Have you tried naming the file create.js.rjs?

Good idea but no difference.

The latest configuration of the create function is:

def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
request.format = :js
end

Gives a page with the address http://localhost:3000/stories/2/comments
it should be http://localhost:3000/stories/2/ and it says this.

Template is missing

Missing template comments/create.erb in view path app/views

Even if I make create.html.erb or as it asks create.erb it makes no
difference. Does that shed any light on the problem?

In peace Neil

The 2 most direct causes I can think of that would produce that error
would be:

  1. create.js.rjs is not in the correct director (…/app/views/
    comments/create.js.rjs
  2. create.js.rjs does not exist or bad file name

Try using:
def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
request.format = :js
respond_to do |format|
format.js
end
end

Although respond _to blocks are not always necessary, it can’t hurt to
try.

Joshua Mckinney wrote:
Try using:

def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
request.format = :js
respond_to do |format|
format.js
end
end

Although respond _to blocks are not always necessary, it can’t hurt to
try.

Too bad, that gives

ActionView::MissingTemplate (Missing template comments/create.erb in
view path app/views):
app/controllers/comments_controller.rb:13:in `create’

Line 13 is respond_to do |format|

So I took it out. create.js.rjs is in views/comments right I think.

In peace Neil

Latest development. I’ve changed the ajax call stories/show.html.erb to

<%= render :partial => 'comment' %>
<h5><label for="login">Make a comment:</label></h5>

<% remote_form_for :comment, :update =>‘aremark’,
:url=>story_comments_path(@story) do |form| %>

<%= form.text_field :body %>

<%= submit_tag ‘Comment’ %>

It still creates the comment in the database.

It correctly returns to http://localhost:3000/stories/2

It even updates the ‘aremark’ div

But it fills it with

Template is missing

Missing template comments/create.erb in view path app/views

Any ideas?

Excuse me while I bang my head on a wall.

In peace Neil

Just to be clear, this is an ajax request correct?

I believe your create method is inside your comments controller, if
this is true then by default rails will look for views associated to
the methods in the comments controller in comments view folder (/
views/comments/…some views…).

You could try to use the render method to specify the location of the
view like (i have not tried this with an ajax request):

def create
@story = Story.find(params[:story_id])
@story.comments.create params[:comment]
request.format = :js
render => ‘/stories/create’
end

or place the create.js.rjs in the comments views folder (not sure
this work given the non-standard url being generated)

or create a custom route.

Joshua Mckinney wrote:

Just to be clear, this is an ajax request correct? TRUE

I believe your create method is inside your comments controller, TRUE

You could try to use the render method to specify the location of the
view like (i have not tried this with an ajax request):

 render => '/stories/create'

gives errors

or place the create.js.rjs in the comments views folder

There is a copy in views/comments but look at the attached screenshot.
The pig is created by the ‘add a fox’ link using
<%= link_to_remote(“Add a fox”,
:url =>{ :action => :add }) %>.
The relevant rjs file is views/stories/add.rjs nb it is in stories and
has a .rjs extension. It evidently works. This suggests to me that the
fault must be in the ajax request. Here it is

<% remote_form_for :comment, :update =>‘aremark’,
:url=>story_comments_path(@story) do |form| %>

<%= form.text_field :body %>

<%= submit_tag ‘Comment’ %>

Can you see anything wrong?

This is add.rjs

page.insert_html :bottom, ‘aremark’, :partial => ‘user/pig’
page.visual_effect :highlight, ‘aremark’, :duration => 8
page.replace_html ‘header’, ‘pig’

In peace Neil

If the request is getting the correct method which creates comment,
then the request is probably working correctly.

I usually try to use the create methods for a model in the
corresponding controller, so if I create a new comment (even if its
originates from the stories view), I simply format my ajax request to
link to the /comments/create method in the comments controller and
build the create.js.rjs or create.js.erb file in the views/comments
directory, that way I can reuse that code elsewhere should the need
arise. This probably works better with the js.erb file since you can
use classes to select elements, instead of just by the element ID that
js.rjs requires.

The only thing I can see that might be wrong in your rjs code is the
location of the partial. Usually, if you are specifying a partial or
view in a different directory the url would look something like ‘’/
users/pig"

Personally I don’t like working with rjs , unless I have to, and I
usually convert that code over to a js.erb file if I can.

Joshua Mckinney wrote:
[…]

Personally I don’t like working with rjs , unless I have to, and I
usually convert that code over to a js.erb file if I can.

Just a short note. I think I share your dislike of RJS (though I have
recently noticed a few things that may make me rethink that opinion),
but I believe that js.erb is a bigger problem. If you’re dynamically
building your JavaScript, something is wrong. You should be able to
pass in variables through the DOM and leave your JS files completely
static (with no need for ERb).

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

At last, took out the :update =>‘aremark’ from the ajax call and used
create.js.rjs in views/comments and it’s working. Thanks to everybody
for your help.