Answer the question
In order to leave comments, you need to log in
Rails 4 How to make changes to params?
Hello
, I have the following situation in the controller, I need to rebuild the hash of the passed parameters. Those. delete some, add new ones. In third rails, this was done in the controller without problems, for example, I could do this:
params[:user].delete('email')
params[:user][:username]='test'
Answer the question
In order to leave comments, you need to log in
You can add to params, for example, in the controller using deep_merge!.
Example:
def card_params
params.require(:card).permit(:original_text, :translated_text, :review_date, :image, :deck_id, deck: [:title]).deep_merge(deck: {user_id: current_user.id})
end
def self.create_with_deck(params)
deck_params = params.delete(:deck)
if params[:deck_id].blank?
deck = Deck.create(deck_params)
params.deep_merge!(deck_id: deck[:id])
end
create(params)
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question