2

Relative newbie here to Ruby on Rails.

Using the standard form_for method in the view for my SomeobjController#new action

= form_for @someobj do |f|
    .
    .
    .
  %p.submits
  = f.submit "Submit", :class => "submit"

a submission param[] array is produced that contains a hash of @someobj for all the fields set in the form, such that

param[someobj] => { "field1" => "val1", "field2" => "val2", ... }

I would prefer to put a different value, the result of someobj.to_s to param[someobj] for the SomeobjController#create to work with, such that

param[someobj] => "strvalfromtos"

I doubt it's relative, but just in case, the model underlying this #new action is not persistent in the database (i.e., Someobj is not derived from ActiveRecord::Base, though some portions of ActiveModel are included.)

I haven't had luck trying to adjust this until after #create is invoked, but its the submission from #new to #create that I want to amend. It's not clear to me if I should be focusing more on the form_for statement or doing something special in the controller (I'm guessing the form_for is the right focus).

And, yes, this whole thing is probably a bit OCD of me, but the actual fieldnames are long (appropriately for the model) but data needed by #create is very small.

Is there a relatively painless way to do this, assuming that someobj.to_s has already been written?

Many thanks, Richard

rdnewman
  • 1,379
  • 20
  • 28
  • Are you using Rails 4? – Beartech Feb 06 '14 at 05:30
  • You question is unclear. You are saying you want the change the value of the params hash? The purpose of the params hash is to pass in data from your form. Are you saying you want to take the form data, turn it into some sort of string, and then submit that? – Beartech Feb 06 '14 at 05:34
  • Sorry, Rails 3.2.16. Yes, I think I am saying that, but your question helps clarify it better for me. I've been thinking it of it more as just as another param, but given the circumstance still seems appropriate. – rdnewman Feb 06 '14 at 05:49
  • @Beartech, thanks for your help too. I appreciate your comments. – rdnewman Feb 06 '14 at 23:27

1 Answers1

0

Change

When you submit the form, your controller will receive the params hash, as you've stated (Rails params explained?)

That means you can change any value in the hash that you wish:

def create
    #has access to the params hash
    params[:owner][:key] = value
end

As the create method receives the hash object, you'll have to change it in here. But because it's a standard hash (which has already been declared), you should be able to alter it as required

Add

If you want to add values to the params hash, you can use the .merge method, like this:

def create
    #has access to the params hash
    params[:key].merge(user_id: current_user.id)
end
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Thank you. I realized that I could affect it in the `create` action, but was wondering if I could affect it coming out of the form even before it got to `create`. In the comments with Beartech (under my OP), he made it clear that what I'm really trying to do is roll up the form data and then submit that as a single string. Your point about the merge into the params hash is especially interesting. – rdnewman Feb 06 '14 at 14:00
  • 1
    I wouldn't try and change the params hash immediately after form submit (goes against convention). I'd use the `before_action :method, only: :create` callback in your controller, which should allow you to modify the passed params as you wish (before the `create` method being called) – Richard Peck Feb 06 '14 at 14:46
  • 1
    That makes sense now. Thank you! I also realized that I could also simplify the params by just abandoning the `form_for` and just use the `form_tag` and associated tags to replace the field names with something much smaller and just create the final object in `create`. Thanks for your help. – rdnewman Feb 06 '14 at 17:33