Ruby - How to define parameters for require ()?

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

I'm completely new to rubies. I followed the guide and he said that I had to take care of some “loopholes” or “security issues” and he used attr_accessible, but on Rails 4 they offer strong options, so now I'm trying to use them. I am confused about how to determine the parameters :questions, because currently I am getting an error that :questionsparam was not found.

:questions - This is pretty much what I will define as a web developer.

, , = " ?", " ?". . , -. , -, . , "", .

? .

+4
2

, ? , params,


4

Strong Params , - . attr_accessible Rails 4.0

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 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 , hash:

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

, - param, require , :question.


  • Question.all(app_params)

, , all. where . , .

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

= " ?", " ?"

, , . , ;


- , , .

"params" , , , . , , . , MVC ( params) : MVC-?

, ,

"" / questions , find. :

#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" :)

+1

question ( ):

params.require(:question).permit(:text, :answer)

, question - , text ( ) - .

0

All Articles