Partial Display Problem in Application Layout (Rails)

Here's the call in the application.html.erb file:

 <%= render :partial => 'tasks/_new' %> 

Here's a partial display ( _new.html.erb ):

 <% form_for @task do |f| -%> <%= f.text_field :body %> <%= submit_tag "Submit" %> <% end -%> 

Here's the method in the task controller:

 def new @task = Task.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @task } end end 

Here is the error message I get:

 Missing template tasks/__new.erb in view path app/views 

And he says the error in this line:

 <%= link_to "tasks", tasks_path %> <%= render :partial => 'tasks/_new' %> 

The file is in the correct directory. The strange thing is that there is extra _ in the file name, in error. When I turn in and rename partly to __new.erb , an error appears here:

 Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id 

And he says the error in this line:

 <% form_for @task do |f| -%> 

I also tried without _ in the code, as Petros suggested, but it returns the same error as above, Called id for nil…

What's happening?

+6
ruby-on-rails forms
source share
3 answers

You do not need _ in your code. It should be:

 <%= render :partial => 'tasks/new' %> 

The first error is that you do not need to embed _ inside: partial parameter. Rails takes care of this. This is why you get double __ because Rails will put it for you.

The second mistake is your real problem. The error indicates that @task is zero. This is true because the partial knows only what the container knows, and your view at that particular moment did not trigger an action from the correct controller. Since you (Baby Diego) have already discovered and indicated in one of your comments below, you need to create an instance of the task in partial. I don’t know if there is a more elegant solution, but maybe someone can offer something better in the future.

Thanks to MattMcKnight for letting us know that the partial only knows what the container knows.

+9
source share

Petros correctly identified the first issue - you do not need an underscore for a partial call. The second thing you need to know about particles is that they don’t call the controller method, they just refer to the view. Thus, you need to configure the @task object in every action that uses this partial, or just call Task.new in partial. When I have a partial layout in such situations, I usually load it using JavaScript so that I can trigger the action.

+4
source share

If partial information needs to know about a variable in the erb calling file, you can pass it as follows:

 <%= render partial: "tasks/new", locals: { task: @task } %> 

And in the file app/views/tasks/_new.html.erb refer to the variable as follows:

 <% form_for task do |f| %> <%= f.text_field :body %> <%= submit_tag "Submit" %> <% end %> 

That is, without @ . (Code a: b is a more convenient form :a => b .)

I wonder why, why do you want to use partials in the application.html.erb file? I assume that you are referring to the file created by Ruby app/views/layouts/application.html.erb , which is supposed to be used as a layout file containing elements common to all of your application pages, and not to business logic. Perhaps the file you need to call partial is app/views/tasks/index.html.erb ?

+1
source share

All Articles