Rails错误参数丢失或值为空:历史记录

时间:2018-12-06 00:34:29

标签: ruby-on-rails ruby ruby-on-rails-5

我试图创建一种功能,在该功能中,我从索引中按下按钮,它将在我创建该按钮的控制器中执行一个动作。一切都很好,直到在我的控制器中他们显示某些参数的值是或为空。感谢您的帮助。 Image Error

我的index.html.erb

<tbody>
  <% undefined = "No definido" %>
  <% @histories.each do |history| %>
  <% if history.history_status == "publicada" %>
   <tr>
    <td><%= history.title %></td>
    <td><%= history.description %></td>
    <td><%= history.history_status %></td>
    <td style="text-align: center;">
     <div class="btn-group">
      <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
        Acciones
        <span class="caret">
        </span>
      </button>
      <ul class="dropdown-menu pull-right" role="menu">
        <li style="background-color:#337ab7; text-align: center;">
           <%= link_to "Ver Historia", admin_history_path(history)%>
        </li>
        <li style="background-color:#d9534f; text-align: center;">
            <%= link_to 'Rechazar Historia', admin_rechazar_historia_path(history), :title => history.title, :description => history.description, method: :patch, data: { confirm: 'Are you sure?' } %>
        </li>
        <li style="background-color:#f0ad4e; text-align: center;">
            <%= link_to 'Eliminar Historia', admin_inactivar_historia_path(history), :title => history.title, :description => history.description, method: :patch, data: { confirm: 'Are you sure?' } %>
        </li>
      </ul>
     </div>
    </td>
  </tr>
 <% end %>  
 <% end %>
</tbody>

我的history_controller.rb

class Admin::HistoriesController < Admin::BaseController

    before_action :set_history, only: [:show, :edit, :update, :destroy, :publicar_historia, :inactivar_historia, :rechazar_historia]

    def new
        @history = History.new
        @image = @history.images.build
    end

    def edit
        @image = @history.images.build
    end

    def create
        @history = History.new(history_params)
        @history.assign_attributes({:user_id => current_user.id})
        @history.assign_attributes({:history_status => 'publicada'})

        if @history.save
        params[:images]['picture'].each do |a|
            @image = @history.images.create!(:picture => a)
        end
            flash[:success] = "Historia creada exitosamente"
            redirect_to admin_histories_path
        else
            flash.now[:danger] = "La Historia no se pudo crear, por favor revise los campos"
            render 'new'
        end
    end

    def show
        @images = @history.images.all
    end

    def index
        @histories = History.all
    end

    def update
        @history.assign_attributes({:history_status => 'publicada'})
        if @history.update(history_params)
            flash[:success] = "Historia actualizada exitosamente"
            redirect_to admin_histories_path
        else
            flash[:warnig] = "Historia no actualizada"
            redirect_to admin_histories_path
        end
    end

    def inactivar_historia
        @history.assign_attributes({:history_status => 'inactiva'})
        if @history.update(history_params)
            flash[:success] = "Historia actualizada exitosamente"
            redirect_to admin_histories_path
        else
            flash[:warnig] = "Historia no actualizada"
            redirect_to admin_histories_path
        end
    end

    def publicar_historia
        @history.assign_attributes({:history_status => 'publicada'})
        if @history.update(history_params)
            flash[:success] = "Historia actualizada exitosamente"
            redirect_to admin_histories_path
        else
            flash[:warnig] = "Historia no actualizada"
            redirect_to admin_histories_path
        end
    end

    def rechazar_historia
       @history.assign_attributes({:history_status => 'rechazada'})
       if @history.update(history_params)
            flash[:success] = "Historia actualizada exitosamente"
            redirect_to admin_histories_path
       else
            flash[:warnig] = "Historia no actualizada"
            redirect_to admin_histories_path
       end
    end

    private
    # Use callbacks to share common setup or constraints between actions.
    def set_history
        @history = History.find(params[:id])
    end

    def history_params
        params.require(:history).permit(:title, :description,images_attributes: [:id, :picture, :history_id])
    end


end

还有我的route.db

Rails.application.routes.draw do
  namespace :admin do
    resources :histories 
  end

  namespace :admin do
    patch "inactivar_historia/:id" => "histories#inactivar_historia", :as => :inactivar_historia
    patch "publicar_historia/:id" => "histories#publicar_historia", :as => :publicar_historia
    patch "rechazar_historia/:id" => "histories#rechazar_historia", :as => :rechazar_historia
  end
end

错误是: ActionController :: ParameterMissing在/ admin / rechazar_historia / 2 参数丢失或值为空:历史记录

_form.html.erb

<%= simple_form_for [:admin, @history], html: {multipart: true} do |f| %>

  <%= f.input :title, label: "Título", required: true, autofocus: true %>

  <%= f.input :description, label: "Descripcion", required: true, autofocus: true %>

  <%= f.fields_for :images do |p| %>
    <div class="field">

      <%= p.label :picture %><br>
      <%= p.file_field :picture, :multiple => true, name: "images[picture][]" %>
    </div>
  <% end %>

 <p>
 <br>

 <div class="actions">
   <%= f.submit %>
 </div>
   

1 个答案:

答案 0 :(得分:0)

为什么要在@history.update(history_paramas)上致电rechazar_historia?您不需要该行,这就是错误。

您具有该操作的链接,因此没有params [:history]。我猜您只是将update操作复制到其他操作上,但是您实际上并不需要在所有操作上更新整个对象,也不需要传递任何数据(并且自操作以来就不应该这样做)不是“更新”,而是“拒绝”。

只需将@history.update(history_params)更改为@history.save,它就会起作用。