【问题标题】:Can't bring content_id through to list of collection and then add to a collection ruby on rails无法将 content_id 带到集合列表,然后添加到 Rails 上的集合 ruby
【发布时间】:2013-08-27 22:50:16
【问题描述】:

太难了!需要有关此 Rails 应用程序的帮助。

对于内容,每个人都可以使用索引和展示内容。内容可以在私有集合中。

我通过设计设置了隐私,但我无法弄清楚如何将 content_id 连接到许多不同的 collection_id。流程是:

点击首页内容

点击转到收藏列表以可能将内容添加到

单击集合上的按钮以添加内容 [这是我一直卡住的地方]

当我点击一个集合时,我可以查看集合中的所有内容,如果我不再需要它,我可以将其删除。

我查看了从 has_and_belongs_to_many 模型、嵌套表单等所有内容,但似乎没有任何内容可以通过 content_id。

另外注意,主视图是home/index.html.erb。

    routes.rb
      resources :contents
      resources :collections
      resources :collections_contents

    collection model
      has_many :collections_contents, dependent: :destroy
      has_many :contents, :through => :collections_contents

    content model
      has_many :collections_contents
      has_many :collections, :through => :collections_contents

    collections_content model
      belongs_to :content
      belongs_to :collection


    Collection and content controllers are the normal scaffold.

    Collections_contents_controller

    def create
      @collection = current_collection
      content = Content.find(params[:content_id])
      @collections_content = @collection.collection_content.build
      @collections_content.content = content

    respond_to do |format|
    if @collections_content.save
       format.html { redirect_to @collections_content.collection, notice: 'Collections content was successfully created.' }
       format.json { render json: @collections_content, status: :created, location: @collections_content }
      else
        format.html { render action: "new" }
        format.json { render json: @collections_content.errors, status: :unprocessable_entity }
        end
      end
    end



    home/index.html.erb
<%  if user_signed_in? %>
  <table>
      <% @collections.each do |collection| %>
        <tr>
          <td><%= collection.name %></td>
          <td><%= collection.thumbnail %></td>
          <td><%= collection.content_number %></td>
          <td><%= link_to 'View Collection', collection %></td>
        </tr>
      <% end %>
  </table>
<% else %>
<% end %>

<%= link_to 'Create New Collection', new_collection_path %>
<table>
  <% @contents.each do |content| %>
    <tr>
      <td><%= content.title %></td>
      <td><%= content.short_description %></td>
      <td><%= content.published_date %></td>
      <td><%= content.image %></td>
      <td><%= link_to 'Read More', content %></td>
      <td><%= link_to 'Add Content to Collection', collections_path(content_id: content) %></td>
    </tr>
  <% end %>
</table>


    <% @collections.each do |collection| %>
      <tr>
       <td><%= collection.name %></td>
       <td><%= collection.thumbnail %></td>
       <td><%= collection.content_number %></td>
       <td><%= link_to 'Add to Collection' %></td>
     </tr>

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3.2 has-and-belongs-to-many


【解决方案1】:

有一个关于如何正确执行此操作的 RailsCast:

http://railscasts.com/episodes/163-self-referential-association

希望它可以帮助那里的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2021-11-30
    • 2019-03-29
    • 2019-02-08
    相关资源
    最近更新 更多