nnyby
nnyby

Reputation: 4668

Rails action not rendering

Here is my index.html.erb:

<%= render :action => 'new' %>

<% unless @posts.empty? %>
  <%= render @posts %>
<% end %>

The posts are displaying, but nothing in the new page is.

It's not in the log either:

Processing PostsController#index (for 127.0.0.1 at 2010-07-27 20:54:28) [GET]
Post Load (0.2ms)   SELECT * FROM "posts" 
Rendering template within layouts/application
Rendering posts/index
Rendered posts/_post (8.4ms)

Also, if I replace it with just <%= render :new %>, I get the error:

undefined method `include?' for :new:Symbol

But it should allow me to render the action implicitly with Rails 2.3.8.

I would be grateful if anyone could explain either of these things.

Upvotes: 0

Views: 258

Answers (2)

Sam Coles
Sam Coles

Reputation: 4043

You should only render partials within a view. Since you need the contents in multiple views, you can convert the contents of new.html.erb to a partial (say, _new.html.erb) and then

<%= render 'new' %>

From new.html.erb as well as index.html.erb. render :action => 'actionname' is meant for rendering another action's template from within the controller.

Note that its a common idiom to create a _form.html.erb partial for each model, and use that whenever you need to be able to add or edit a model instance from elsewhere. That would be useful in this case as well. You would then render the form partial from index, new and edit.

Upvotes: 1

JasonTrue
JasonTrue

Reputation: 19599

Just a thought, but I suspect that erb will treat those two blocks as a single line. As you probably know, in Ruby it's quite idiomatic to put an "unless" or "if" condition at the end of a line.

You may want to add a line break before "unless" but after <%.

Upvotes: 0

Related Questions