Quiero editar un registro con una acción en el controlador pero al pasar el registro llega nil
y por supuesto no lo puedo editar. Adjunto el controlador, el modelo y la vista.
Se puede ver el código en GitHub (es un código muy básico generado con Scaffold), solo quiero destacar el error que me da.
Estoy usando las siguientes versiones en local:
- ruby 2.2.6p396 (2016-11-15 revision 56800) [i386-mingw32]
- Rails 5.1.1
app\controllers\articulo_controller.erb
def ajustar
puts '------------------------------------------------------------'
puts @articulo.inspect
# @articulo.contenido = "ref:" + @articulo.contenido
puts '------------------------------------------------------------'
redirect_to articulos_url
end
app\config\routes.rb
Rails.application.routes.draw do
resources :articulos
post '/ajustar/:id' => 'articulos#ajustar', as: 'ajustar'
end
app\view\index.html.erb
<% @articulos.each do |articulo| %>
<tr>
<td><%= articulo.nombre %></td>
<td><%= articulo.titulo %></td>
<td><%= articulo.contenido %></td>
<td><%= link_to 'Show', articulo %></td>
<td><%= link_to 'Edit', edit_articulo_path(articulo) %></td>
*<td><%= link_to 'ajusar', **ajustar_url(articulo)**, method: :post %></td>*
<td><%= link_to 'Destroy', articulo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
salida del log
Started POST "/ajustar/1" for 127.0.0.1 at 2017-06-23 13:29:28 -0400
Processing by ArticulosController#ajustar as HTML
------------------------------------------------------------ nil
------------------------------------------------------------ Redirected to http://localhost:3000/articulos Completed 302 Found in
6ms (ActiveRecord: 0.0ms)
Started GET "/articulos" for 127.0.0.1 at 2017-06-23 13:29:28 -0400
Processing by ArticulosController#index as HTML Rendering
articulos/index.html.erb within layouts/application Articulo Load
(2.0ms) SELECT "articulos".* FROM "articulos" Rendered
articulos/index.html.erb within layouts/application (12.0ms) Completed
200 OK in 640ms (Views: 565.1ms | ActiveRecord: 2.0ms)