Add action to the controller and invoke with link_to in the view: - ERROR Completed 400 Bad Request

1

In the classic application blog article ...

I want to add an action in the article driver adjust by calling it post , basically I update the text column of the article method by adding just one asterist (*) each time it is adjusted, but I get the following error:

Started POST "/ajustar/5" for ::1 at 2017-06-30 13:03:51 -0400
Processing by ArticlesController#ajustar as HTML
  Parameters: {"authenticity_token"=>"rTCc5efVQSm5GG1b47uPH7U+xJq9Vh+N9mUvKpn5WDLf0zqBNiwcbOjvkiyIH8
Ccph6rYh0pH/09hGbq9bZq5Q==", "id"=>"5"}
  Article Load (0.0ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [[
"id", 5], ["LIMIT", 1]]
Completed 400 Bad Request in 9ms (ActiveRecord: 1.0ms)



ActionController::ParameterMissing (param is missing or the value is empty: article):

app/controllers/articles_controller.rb:59:in 'article_params'
app/controllers/articles_controller.rb:37:in 'ajustar'
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatc
h/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatc
h/middleware/templates/rescues/_source.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatch
/middleware/templates/rescues/_source.html.erb (8.0ms)
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatc
h/middleware/templates/rescues/_trace.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatch
/middleware/templates/rescues/_trace.html.erb (6.0ms)
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatc
h/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatch
/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.2/lib/action_dispatch
/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (2516.1ms)

Code snippets:

Vista

D: \ Sites \ article-adjust \ app \ views \ articles \ index.html.erb

<table>
  <tr>
    <th>Titulo</th>
    <th>Articulo</th>
    <th colspan="7"></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Mostrar', article_path(article) %></td>
        <td><%= link_to 'Actualizar', edit_article_path(article) %></td>
      <td><%= link_to 'Ajustar', ajustar_path(article), method: :post %></td>
        <td><%= link_to 'Borrar', article_path(article),
              method: :delete,
              data: { confirm: 'Esta seguro?' } %></td>
    </tr>
  <% end %>
</table>

Driver

D: \ Sites \ article-adjust \ app \ controllers \ articles_controller.rb

    def ajustar
        @article = Article.find(params[:id])
        @article.text = '*' + @article.text
        @article.update(article_params)
    end
...
    private
        def article_params
            params.require(:article).permit(:title, :text)
        end

Route

D: \ Sites \ article-adjust \ config \ routes.rb

Rails.application.routes.draw do
...
  post  '/ajustar/:id' => 'articles#ajustar', as: 'ajustar'
...
end

Annex in github all the code of the application

link

    
asked by rrg1459 30.06.2017 в 19:43
source

1 answer

1

If you are only going to add an asterisk to a field, you do not need to access article_params , since that implies that you will require parameters that you are not sending in your POST . Your code should be like:

def ajustar
    @article = Article.find(params[:id])
    @article.update(text: "*#{@article.text}")
end
    
answered by 30.06.2017 / 21:47
source