Junaid Farooq
Junaid Farooq

Reputation: 2608

Rails - ActionController::Parameters to string

There are two params and am getting a single one like that

v = (params.require(:service).permit(:title))

This v has a value like this {"title"=>"test,kil"} and I want to get the value of right side to a string, but all in vain. The concept am trying to impose after that i will have @test= "test,kil"

Anyhelp will be great full. am a newbie to Rails.

Upvotes: 1

Views: 5802

Answers (2)

Dan Draper
Dan Draper

Reputation: 1121

You can just do the following:

@test = params.require(:service).permit(:title)[:title]

def create
  @qure = params.require(:service).permit(:level_id)[:level_id]
  @test = params.require(:service).permit(:title)[:title]
  @gain = @test.split(",")

  # Note: You need to use a hash to pass args to the model
  @gain.each do |fil|
    Service.create!(title: fil, level_id: @qure)
  end

  redirect_to root_url
end

I've also used create! in the method but you should consider using save and checking the return value if you want to use validations. The above will raise an error if there are validation issues.

Upvotes: 0

max
max

Reputation: 102249

params in Rails is just a Hash (well kind of).

To access the values of a hash in Ruby you use the square bracket syntax:

v = params.require(:service).permit(:title)
@test = v['title']

Params in Rails are a kind a special hash type called HashWithIndifferentAccess. So we can do both v['title'] or v[:title].

Upvotes: 1

Related Questions