加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby – 如何为require()定义params?

发布时间:2020-12-17 02:05:22 所属栏目:百科 来源:网络整理
导读:questions_controller.rb def index @questions = Question.all(app_params) end private def app_params params.require(:questions).permit(:question,:answer) endend question.rb class Question ActiveRecord::Baseend 我对ruby-on-rails完全不熟悉.我
questions_controller.rb

def index
    @questions = Question.all(app_params)
  end

  private

  def app_params
    params.require(:questions).permit(:question,:answer)
  end
end

question.rb

class Question < ActiveRecord::Base
end

我对ruby-on-rails完全不熟悉.我正在关注一个指南,它说我应该处理一些“漏洞”或“安全问题”并使用attr_accessible,但在Rails 4上,他们提出了强大的参数,所以现在我正在尝试使用它们.我很困惑如何定义:问题参数,因为我目前收到的错误是:找不到问题参数.

:问题几乎是我将自己定义为Web开发人员的东西.

例如,我将定义问题=“你好吗?”,“你叫什么名字?”.我基本上很简单.我想要创建的问题显示在我的网页上.最终,我计划建立一个基本上是问题列表的网站,并提供答案选项.用户点击“提交”后,我想将信息存储到我的数据库中.

我是否应该要求这作为一个参数?我完全失去了..

解决方法

你有我们可以看到的参数转储吗?当您的应用遇到错误时会显示它们,并且通常会显示哪些轨道将通过的params数组

在路轨4强的参数

Strong Params允许您允许在控制器中使用某些参数,以防止任何恶意分配客户端.他们取代了Rails 4.0中的attr_accessible

Strong Params仅适用于用户提交的内容,因为它旨在保护params散列.为此,它主要用于create和find功能:

class PeopleController < ActionController::Base
  # Using "Person.create(params[:person])" would raise an
  # ActiveModel::ForbiddenAttributes exception because it'd
  # be using mass assignment without an explicit permit step.
  # This is the recommended form:
  def create
    Person.create(person_params)
  end

  # This will pass with flying colors as long as there's a person key in the
  # parameters,otherwise it'll raise an ActionController::MissingParameter
  # exception,which will get caught by ActionController::Base and turned
  # into a 400 Bad Request reply.
  def update
    redirect_to current_account.people.find(params[:id]).tap { |person|
      person.update!(person_params)
    }
  end

  private
    # Using a private method to encapsulate the permissible parameters is
    # just a good pattern since you'll be able to reuse the same permit
    # list between create and update. Also,you can specialize this method
    # with per-user checking of permissible attributes.
    def person_params
      params.require(:person).permit(:name,:age)
    end
end

params.require

params.require函数通过使用此params哈希来工作:

params{:question => {:question => "1",:answer => "5"}}

这就是为什么人们会问你的params hash看起来是什么样的,因为require函数只能在:question hash存在时才能工作.

可能的解决方案

> Question.all(app_params)

无论你想要达到什么目的,都不要全部使用. where函数更适合根据特定值接收数据数组.我相信all is depreciated anyway.

def index
    @questions = Question.where("value = ?",variable)
end

>正在传递什么数据?

I will define questions = “How are you?”,“What is your name?”

这没关系,但通常在rails中,您可以使用数据库中的ID来调用数据.如果你在一个表格中定义这些问题,你就会使用强大的params系统;但是您需要一个表单来提交数据

进一步补充

rails方式是将所有数据保存在数据库中,并使用应用程序通过显示数据或允许人们输入更多数据来操作数据.

“params”变量基本上可以帮助轨道控制器和模特接受&处理来自最终用户的数据,从而使您可以保持系统不断增长.这些参数不是必须编写自定义代码来容纳各种不同的数据,而是为您提供了一个严格的结构.以下是对MVC(和params)如何为您工作的一个很好的解释:How does an MVC system work?

我认为你对应用程序应该如何运作感到困惑

您的“问题”应存储在问题表/模型中,并可通过使用find函数调用其ID来访问.这段代码是这样的:

#app/controllers/questions_controller.rb
def show
    @question = Question.find(params[:id])
end

如果您想添加新问题,最好将它们添加到问题表中,如下所示:

#app/controllers/questions_controller.rb
def new 
    @question = Question.new
end

def create
    @question = Question.new(question_params)
    @question.save
end


private
def question_params
    params.require(:question).permit(:question)
end


#app/views/questions/new.html.erb
<%= form_for @question do |f| %>
    <%= f.text_field :question %>
<% end %>

这将为您提供问题的中央存储,然后您可以在需要时访问这些问题,使用helper或“.all”调用:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读