单击按钮时从poro属性创建模型

时间:2020-11-30 20:43:20

I'm making an API call and populating my view with the POROs generated. When someone clicks 'Favorite', I want it to create the model and persist the information to the DB. I'm having trouble bridging the gap, since the only thing I can pass through the params is the id and I can't find anything in the controller since the PORO isn't persisted to the DB. Here's the link. It's inside an iteration block of the PORO collection. I can't figure out what to pass to the path helper.

我正在进行API调用并使用生成的PORO填充我的视图。当有人点击“收藏夹”时,我希望它创建模型并将信息保存到数据库中。我在弥合差距方面遇到了麻烦,因为我唯一可以通过params的是id,我在控制器中找不到任何东西,因为PORO没有持久保存到DB。这是链接。它位于PORO集合的迭代块中。我无法弄清楚要传递给路径助手的内容。

 <%= link_to 'Favorite', favorite_path(), method: :patch %>

here's the PORO.

这是PORO。

class VideoService
  attr_reader :etag, :video_id, :img_high,
              :img_default, :title,
              :published_at, :description



    def initialize(attrs)
        @etag         = attrs[:etag]
        @video_id     = attrs[:id][:videoId]
        @img_high     = attrs[:snippet][:thumbnails][:high][:url]
        @img_default  = attrs[:snippet][:thumbnails][:default][:url]
        @title        = attrs[:snippet][:title]
        @published_at = attrs[:snippet][:published_at]
        @description  = attrs[:snippet][:description]
      end
    end

I've looked at how to pass the whole poro but all I've found is that you can only pass the id to the controller. And I don't want to call Video.create(...) in the view. Thanks for any light you all are able to shine on this.

我已经看过如何通过整个poro但我发现的只是你只能将id传递给控制器​​。而且我不想在视图中调用Video.create(...)。感谢任何光线,你们都能发光。

1 个解决方案

#1


1  

You could use a form with hidden inputs and then submit button

您可以使用带有隐藏输入的表单,然后提交按钮

#1


1  

You could use a form with hidden inputs and then submit button

您可以使用带有隐藏输入的表单,然后提交按钮