简体   繁体   English

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

[英]Rails error param is missing or the value is empty: history

I am trying to create a functionality in which from my index I press a button and it will execute an action in the controller in which I made it. 我试图创建一种功能,在该功能中,我从索引中按下一个按钮,它将在我创建该按钮的控制器中执行一个动作。 Everything is fine until in my controller they show that the values ​​of certain parameters are or are empty. 一切都很好,直到在我的控制器中他们显示某些参数的值是或为空。 I appreciate the help. 感谢您的帮助。 Image Error 影像错误

My index.html.erb 我的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>

My history_controller.rb 我的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

And my route.db 还有我的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

The error is: ActionController::ParameterMissing at /admin/rechazar_historia/2 param is missing or the value is empty: history 错误为:/ admin / rechazar_historia / 2参数中的ActionController :: ParameterMissing丢失或值为空:history

The _form.html.erb _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>

Why are you calling @history.update(history_paramas) on rechazar_historia ? 为什么要在rechazar_historia上调用@history.update(history_paramas) you don't need that line and that's the error. 您不需要该行,这就是错误。

You have a link to that action so there's no params[:history]. 您具有该操作的链接,因此没有params [:history]。 I guess you just copied the update action on for the other actions, but you don't really need to update the whole object on all the actions, you are not passing any data (and you shouldn't since the action is not "update", it's "reject"). 我猜您只是将update操作复制到其他操作上,但是您实际上并不需要在所有操作上都更新整个对象,您没有传递任何数据(并且不应这样做,因为该操作不是“更新” ”,这是“拒绝”)。

Just change those @history.update(history_params) with @history.save and it should work. 只要改变这些@history.update(history_params)@history.save ,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM