【问题标题】:Rails 4:Simple-form checkbox for active and inactive postRails 4:活动和非活动帖子的简单形式复选框
【发布时间】:2015-05-03 17:36:41
【问题描述】:

已经创建了一个复选框 - 简单形式的布尔函数,想知道控制器如何在索引指示时隐藏帖子。 当用户创建帖子时,他可以选择从所有帖子列表 (index.html.haml) 中隐藏该帖子。在表单中我创建了活动布尔输入,因此如果活动为真,则帖子会显示在索引中,如果活动为假,则帖子将从索引中隐藏。

= simple_form_for @post do |f|
	= f.input :post
	= f.input :active ,:input_html => { :checked => true }
	= f.submit

index.html.haml

- @posts.each do |post|
	%h2.post= link_to post.post, post



class PostsController < ApplicationController
	before_action :find_post, only: [:show, :edit, :update, :destroy]
	before_action :authenticate_user!,except:[:index,:show]

	def index
		@posts = Post.all.order("created_at DESC")
		@post = Post.new

	end

【问题讨论】:

  • 请详细说明你想做什么。
  • @Marwen 当用户创建帖子时,他可以选择从所有帖子列表 (index.html.haml) 中隐藏该帖子。在我创建了活动布尔输入的表单中,如果活动为真,则帖子会显示在索引中,如果活动为假,则帖子将从索引中隐藏。

标签: ruby-on-rails ruby-on-rails-4 checkbox haml simple-form


【解决方案1】:

如果我得到你想要做的,那就是隐藏indexaction上的非活动帖子

def index
  @posts = Post.where(active: true).order("created_at DESC")
  @post = Post.new
end

【讨论】:

  • 我尝试添加一个名为“views”的新页面,并在控制器中添加了 def view @posts = Post.all.order("created_at DESC")
  • 出现错误找不到带有 'id'=view 的帖子 ...指向 def find_post @post = Post.find(params[:id]) end
猜你喜欢
  • 2015-07-27
  • 2012-03-27
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
相关资源
最近更新 更多