Vista parcial de Ruby on Rails

Tengo 3 modelos:

  • cliente
  • book_manager
  • primer libro

La opción customer can edit its own setting, like a profile account. However, it renders different models, such has book. To do this I decided to render the file book/form has follow <% render book/form %>.

When the profile page loads up, the customer may view in the book sections either nothing if nothing has been written before, or something if he already wrote something before. In the customer controller page I have the following code to fetch the information:

def edit
  @book = current_customer.books.order("created_at DESC").limit(1)

En la columna books/_form.html.erb I have then the following:

<%= form_for(@book) do |f| %>
  <% if @book.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>

      <% @book.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
  <% end %>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  <div class="actions">
    <%= f.submit %>
<% end %>

In book controlador edit, I have nothing or where should I place my command @book so they can speak?

Tengo el siguiente error:

NoMethodError in Customers#edit

Showing /home/jean/rail/map/app/views/books/_form.html.erb where line #1 raised:

undefined method `model_name' for ActiveRecord::Relation:Class
Extracted source (around line #1):

1: <%= form_for(@book) do |f| %>
2:   <% if @book.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>

preguntado el 31 de julio de 12 a las 15:07

2 Respuestas

<% render book/form.html.erb %>

debiera ser

<%= render '/books/form' %>

Respondido 31 Jul 12, 15:07

Oops yeah thanks, that me writing it down on the forum, my bad edited - Jseb

You have at least one simple error in your code

@book = current_user.books.order("created_at DESC").limit(1)

This will indeed return the most recent book, but it will return it in an array.

En lugar de llamar limit(1) deberías usar el método first

@book = current_user.books.order("created_at DESC").first

Respondido 31 Jul 12, 15:07

Thanks, i have one problem what would be the best if its empty? cause my new error is undefined method `model_name' for NilClass:Class - Jseb

The normal approach is in the new acción hacer @book = en la edit acción hacer @book = current_user.books.find(params[:id]). In both these cases saying form_for @book works fine. Maybe you should try something similar. - matthew rudy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.