Ajeet Khan
Ajeet Khan

Reputation: 9190

Count comma separated value passed as parameter to controller in rails

In the rails application, the textarea have the comma separated values and while saving the form. I want to save the the count of comma separated value to the database instead of the whole string. How to count this. The textarea in view is:

<%= f.text_area :sent_to %>

The parameters being passed to the controller are:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"ag8WL59Swqo1TOFYfMOAC55VgoIxGSkleMXFDhUXYhO91zcPngas26MbpvMMyydNPSYntCfjAvKSh2R0TdQtbA==", "message_text"=>"    ", "sms_type"=>"true", "staff_type"=>"Teaching", "message"=>{"sent_to"=>"34434343243, 7869851872", "organization_id"=>"4"}, "contact_nos"=>["34434343243, 7869851872"], "contact_no"=>["34434343243", "7869851872"]}

Now the controller have message_params action

 def message_params
      params.require(:message).permit(:message_text, :sent_to, :no_of_message, :organization_id)
    end

Following is the create action to save the data

def create
    @message = Message.new(message_params)

    respond_to do |format|
      if @message.save
        format.html { redirect_to @message, notice: 'Message was successfully created.' }
        format.json { render :show, status: :created, location: @message }
      else
        format.html { render :new }
        format.json { render json: @message.errors, status: :unprocessable_entity }
      end
    end
  end

Thank you all for your answer, the problem is solved and one more thing I want the staff_type to be stored in sent_to. For this I did but it didn't worked. @message.sent_to = message_params[:message_text][:staff_type]

Upvotes: 2

Views: 1182

Answers (5)

BinaryMee
BinaryMee

Reputation: 2142

You could do

def create
    @message = Message.new(message_params)
    @message.sent_to = @message.sent_to.try(:split,",").try(:count)
end

Update

From your comment I guess that, you want to save both sent_to count and staff_type in same attribute sent_to??

You can save only one at a time, Or you have to serialize the attribute and you can save sent_to and staff_type as an object. But if you have too many search queries using sent_to it is not recommended to use serialized column.

If you want to save staff_type alone, you should use

@message.sent_to = params[:staff_type]

Upvotes: 2

Milind
Milind

Reputation: 5112

storing other values along with sent_to

        number_array=params[:message][:sent_to]
        @final_array=[]
        number_array.split(",").flatten.compact.each do |number|
          @final_array << {:number => number, :staff_type => message_params[:message_text][:staff_type]}
          ##something like @final_array[0] will give
          ##{:123456=>123456,:staff_type=>Teaching}
          ##@final_array[0][:12345] will give 12345
          ##@final_array[0][:staff_type] will give "Teaching"
        end

Upvotes: 0

Rubyrider
Rubyrider

Reputation: 3587

You shouldn't perform such operation in controller. The more standard way is to perform this in your model. Create a before validation callback method and spite it to append count to its origin. In this way you wont have to worry about while updating same action from different levels of url.

Update your params permission:

before_validation: :append_send_to_count

def append_send_to_count
    if sent_to.present? and sent_to_changed?
       seld.sent_to_count = sent_to.split(',').size
    end
end

Upvotes: 0

Akshay Borade
Akshay Borade

Reputation: 2472

Try this

count = params[:message]['sent_to'].split(",").count

Hope this will work for you.

Upvotes: 0

Andrew
Andrew

Reputation: 203

This should work.

sent_to_count = message_params[:message][:sent_to].split(",").count

Upvotes: 0

Related Questions