Rails,链接页面

时间:2017-01-15 23:45:51

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

我需要一些帮助。

我有«竞争者»属于帖子。 (每个帖子都有不同的竞争者)。

我希望能够在点击某个帖子时显示竞争者。

我不知道如何链接/路由此。我希望将«帖子»作为我的索引页面,并且能够点击任何帖子并拥有相应的竞争者。 我向竞争者添加了一个post_id作为参考。但我甚至不知道这是否正确。

感谢您的帮助!!

PS:我不知道这是否会改变,但我正在尝试使用ActiveAdmin作为后端和Carrierwave来上传图像。

文章/ index.html.erb

const blockCount = 7;
const block = Array.from({ length: blockCount }, (_, i) => {
  return Array.from({ length: blockCount }, (_, j) => 'Some Value');
});

console.log(block);

竞争者/ index.html.erb

<div class="container">

        <div class="row">

            <div class="col-lg-12">
                <h1 class="page-header">Your poll app</h1>
            </div>
            <% @posts.each do |post| %>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <%= post.name %>
                    <br>
                    <%= post.id %>
                    <%= link_to "Contenders", post_contenders_path(post.id) %>
                    <img class="img-responsive" src="" alt="">
                     <%= image_tag post.image_url %>
                    </img>
                </a>
            </div>
            <% end %>
        </div>
</div>

控制器/ contenders_controllers.rb

class ContendersController < InheritedResources::Base
     def index
        @contenders = Contender.all
     end

     def new
        @contender = Contender.new
     end

     def create
        @contender = Contender.new(post_params)

        if @contender.save
           redirect_to posts_path, notice: "The contender #{@contender.name} has been uploaded."
        else
           render "new"
        end

     end

     def destroy
        @contender = contender.find(params[:id])
        @contender.destroy
        redirect_to posts_path, notice:  "The post #{@contender.name} has been deleted."
     end

      private
         def post_params
         params.require(:contender).permit(:name, :image)
      end

    private

      def contender_params
        params.require(:contender).permit(:name, :image, :post_id)
      end
end

控制器/ posts_controllers.rb

class ContendersController < InheritedResources::Base
     def index
        @contenders = Contender.all
     end

     def new
        @contender = Contender.new
     end

     def create
        @contender = Contender.new(post_params)

        if @contender.save
           redirect_to posts_path, notice: "The contender #{@contender.name} has been uploaded."
        else
           render "new"
        end

     end

     def destroy
        @contender = contender.find(params[:id])
        @contender.destroy
        redirect_to posts_path, notice:  "The post #{@contender.name} has been deleted."
     end

      private
         def post_params
         params.require(:contender).permit(:name, :image)
      end

    private

      def contender_params
        params.require(:contender).permit(:name, :image, :post_id)
      end
end

终端

在2017-01-16 20:22:16 +0000开始获取127.0.0.1的GET“/ posts / 10 / contenders” 由ContendersController处理#index为HTML   参数:{“post_id”=&gt;“10”}   在布局/应用程序中渲染竞争者/ index.html.erb   竞争者负载(0.5ms)
选择“竞争者”。*来自“竞争者”   在布局/应用程序中呈现竞争者/ index.html.erb(2.8ms) 在177ms完成200 OK(浏览次数:172.9ms | ActiveRecord:0.5ms)

在2017-01-16 20:35:30 +0000开始获取127.0.0.1的“/” 由PostsController处理#index为HTML   在布局/应用程序中渲染posts / index.html.erb   后载(1.0ms)
选择“帖子”。* FROM“帖子”   在布局/应用程序中呈现posts / index.html.erb(25.3ms) 在322ms完成200 OK(浏览次数:310.8ms | ActiveRecord:1.0ms)

所以我编辑了代码。它仍然没有表现出正确的东西。

文章/ index.html.erb

class PostsController < ApplicationController
     def index
        @posts = Post.all
     end

     def new
        @post = Post.new(params[:id])
     end

     def create
        @post = Post.new(post_params)

        if @post.save
           redirect_to posts_path, notice: "The post #{@post.name} has been uploaded."
        else
           render "new"
        end

     end

     def destroy
        @post = Post.find(params[:id])
        @post.destroy
        redirect_to posts_path, notice:  "The post #{@post.name} has been deleted."
     end

     private
        def post_params
        params.require(:post).permit(:name, :image)
     end

end

竞争者/ index.html.erb

<div class="container">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Your poll app</h1>
            </div>
            <% @posts.each do |post| %>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <%= post.name %>
                    <br>
                    <%= post.id %>
                    <%= link_to "Contenders", post_contenders_path( post_id: post.id ) %>
                    <img class="img-responsive" src="" alt="">
                     <%= image_tag post.image_url %>
                    </img>
                </a>
            </div>
            <% end %>
        </div>
</div>

控制器/ contenders_controllers.rb

<div class="container">

        <div class="row">

            <div class="col-lg-12">
              <h1 class="page-header">
                Test
              </h1>
            </div>
            <% @contenders.each do |contender| %>
            <div class="col-lg-3 col-md-4 col-xs-6 thumb">
                <a class="thumbnail" href="#">
                    <%= contender.name %>
                    <br>
                    <%= contender.post_id %>
                    <img class="img-responsive" src="" alt="">
                     <%= image_tag contender.image_url %>
                    </img>
                </a>
            </div>
            <% end %>
        </div>
</div>

控制器/ posts_controllers.rb

class ContendersController < InheritedResources::Base
     def show
        @post = Post.find_by(id: params[:post_id] )
        @contenders = @post.contenders
     end

      private
         def post_params
         params.require(:contender).permit(:name, :image)
      end

    private

      def contender_params
        params.require(:contender).permit(:name, :image, :post_id)
      end
end

的routes.rb

class PostsController < ApplicationController
     def index
        @posts = Post.all
     end
     def show
       @post = Post.find_by(id: params[:post_id] )
       @contenders = @post.contenders # Assuming that they have a has_many/belongs to relationship.
     end

     private
        def post_params
        params.require(:post).permit(:name, :image)
     end

end

1 个答案:

答案 0 :(得分:0)

如果你想拥有一个嵌套的url,那么你需要在你的路径文件中看到如下内容:

# routes.rb
resources :posts do
  resources :contenders
end

然后在您的posts_controller中,您希望show方法能够成为特定contenders的{​​{1}}索引,对吗?

因此,在show方法中,您让控制器找到包含post的帖子,然后列出该帖子的竞争者,如下所示:

:post_id

至于视图,您看起来已经正确地链接了它。您可以在命令行上运行# posts_controller.rb def show @post = Post.find_by(id: params[:post_id] ) @contenders = @post.contenders # Assuming that they have a has_many/belongs to relationship. end 以获取应用程序已绘制的路由的输出。第一列是您可以为路径调用的名称列表。在第三列中,您会看到包含rake routes:id等任何动态部分的网址,这些部分会告诉您需要将该信息传递给路线。

因此,如果您有一个类似:post_id的网址,这将是您的索引操作,则需要/posts/:post_id/contenders,如下所示:

link_to

然后,在您的竞争者显示页面中,您有类似的内容(使用上面的@contenders变量):

<% @posts.each do |post| %>
  ...
  <%= link_to 'Post', post_contenders_path( post_id: post.id ) %>
  ...
<% end %>

让我知道结果如何!

编辑:

您似乎正在渲染# views/contenders/show.html.erb ... <% @contenders.each do |contender| %> ... <%= contender.name %> <!-- Or some other attribute that contender has... --> ... <% end %> ... 而不是contenders#index。如果您想以这种方式执行此操作,那么您可以将竞争者的索引操作范围限定在帖子中。像这样:

post#show