【问题标题】:Trying to archive a specific feedback in Rails 4尝试在 Rails 4 中归档特定反馈
【发布时间】:2016-05-18 10:54:43
【问题描述】:

我已经创建了一个反馈系统,并正在尝试将特定的反馈归档。我尝试了多种不同的配置,但似乎无法让它工作。我没有收到错误,并且在我收到通知时它似乎正在触发控制器操作。但是,布尔值在 DB 中不会改变,特定的反馈也不会隐藏。

任何帮助将不胜感激!

  • 我在表中添加了一个存档布尔值。
  • 我创建了存档路径。
  • 我已经为存档创建了控制器操作。

这是我的代码:

**Controller:**

def archive_feedback
 @listing_feedback = ListingFeedback.find(params[:id])

 respond_to do |format|
   format.html { redirect_to listing_listing_feedbacks_path, notice: "That feedback has been archived." }
   format.json { render :index }
 end
end

**Routes: (My feedback feature is using nested resources)**

resources :listings do
  member do
   get 'like'
   get 'unlike'
   get 'duplicate'
   get 'gallery'
   delete 'gallery' => 'listings#clear_gallery'
   get 'manage_photos'
   get 'craigslist'
   get "add_to_collection"
  end
  resources :listing_feedbacks do
    member do
     put 'archive_feedback'
  end
 end
end

**Index.html.erb:**

<p><%= link_to 'Archive', controller: "listing_feedbacks", action: "archive_feedback", id: listing_feedback.id, archive: :true, method: :put %></p>

另外,存档后如何隐藏反馈?

【问题讨论】:

  • 你没有在控制器中保存任何东西......试试@listing_feedback.update_attributes(:attribute => value)
  • 您要更新的属性名称是什么?只需在控制器上更新

标签: ruby-on-rails ruby-on-rails-4 archive


【解决方案1】:

您在archive_feedback 方法中没有更新 @listing_feedback。改成下面

def archive_feedback
 @listing_feedback = ListingFeedback.find(params[:id])
 @listing_feedback.update(archive: true)

 respond_to do |format|
   format.html { redirect_to listing_listing_feedbacks_path, notice: "That feedback has been archived." }
   format.json { render :index }
 end
end

【讨论】:

    【解决方案2】:

    您是否将新的存档属性添加到控制器中的请求参数中?
    如果不是,则不会保存到数据库中。

    private
    def listing_feedback_params  
    params.require(:listing_feedback).permit :archive  
    end
    

    【讨论】:

      【解决方案3】:
      I think ..
      
      you have to write as below in controller
      
      
      def archive_feedback
       @listing_feedback = ListingFeedback.find(params[:id])
      
       @listing_feedback.destroy
         or
       @listing_feedback.update_attribute(archive: true)
      
       respond_to do |format|
         format.html { redirect_to listing_listing_feedbacks_path, notice: "That feedback has been archived." }
         format.json { render :index }
       end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        • 2019-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多