I have a form like
<input name="url[0]" type="text" />
<input name="url[1]" type="text" />
<input name="url[2]" type="text" />
I would like to be able to access these like:
params[:url].each do |url|
# work
end
I know that if I remove the explicit index from the name, this will work, but I would prefer to keep the index in. Is this something supported by rails out of the box?
You need to modify your block like so:
params[:url].each do |index, url|
# work
end
params[:url] will be an hash like this:
params[:url]
=> {'0' => 'url-1', '1' => 'url-2', '2' => 'url-3'}
So you have to iterate over the hash like this:
params[:url].each do |key, value|
# work
end
Related
I need to pass an array in a params, possible? Values can be, for example, ["1","2","3","4","5"] and these are strings but needs to eb converted to integers later.
I use a react_component in between a rails form_for. The html is like this:
<input type="hidden" name="people_id" id="people_id" value={this.state.people} />
The people array looks like this:
How can I pass the array in the value of the hidden field? The server error I got was
Im trying to do something like this in a model:
ids = params[:people_id]
ids.map do |b|
Foo.create!(people_id: b.to_i)
end
If I ids.split(",").map I get symbol to int error.
Edit:
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Still not sure what the issue is as nothing works. Here is a minimal reproduction of my code:
This answer is my react component and that's how I add to the array. Still in the component, I have the hidden field:
<input type="hidden" name="[people_id][]" id="people_id" value={this.state.people} />
_form.html.erb:
<%= form_for resource, as: resource_name, url: registration_path(resource_name), :html => { :data => {:abide => ''}, :multipart => true } do |f| %>
<!-- react component goes here -->
<%= f.submit "Go", class: "large button" %>
<% end %>
The story is, guest can select few people during registration in one go. Those people will be notified when registration is complete. Think of it as "I am inviting these people to bid on my tender". Those numbers, in the array, are user_ids.
users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
# POST /resource
def create
super do |resource|
ids = params[:people_id].pop # logs now as "people_id"=>["1,2"]
resource.save!(ids.split(",").map |b| Foo.create!(people_id: b.to_i) end)
end
end
end
New error on line resource.save:
no implicit conversion of Symbol into Integer
Edit #2
If I only have, in the create method:
ids.split(",").map do |b|
resource.save!(Foo.create!(people_id: b.to_i))
end
It works! Foo is created two times each with the correct people_id.
Because I am creating more objects: Bar, I do not know how to do that in:
resource.save!(<the loop for Foo> && Bar.create!())
The flow must be:
Device creates the User
Foo is created with the loop
Bar is created
etc
It has to be done that way as an User object is created on the fly.
In Rails you use parameter keys with brackets on the end to pass arrays.
However you should not concatenate the values as a comma seperated list but rather send each value as a seperate param:
GET /foo?people_ids[]=1&people_ids[]=2&people_ids[]=3
That way Rails will unpack the parameters into an array:
Parameters: {"people_ids"=>["1", "2", "3"]}
The same principle applies to POST except that the params are sent as formdata.
If you want a good example of how this works then look at the rails collection_check_boxes helper and the inputs it generates.
<input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
<label for="post_author_ids_1">D. Heinemeier Hansson</label>
<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
<label for="post_author_ids_2">D. Thomas</label>
<input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
<label for="post_author_ids_3">M. Clark</label>
<input name="post[author_ids][]" type="hidden" value="" />
Updated:
If you intend to implement you own array parameters by splitting a string you should not end the input with brackets:
<input type="hidden" name="[people_id][]" value="1,2,3">
{"people_id"=>["1,2,3"]}
Notice how people_id is treated as an array and the input value is the first element.
While you could do params[:people_id].first.split(",") it makes more sense to use the correct key from the get go:
<input type="hidden" name="people_id" value="1,2,3">
Also you don't really want to wrap the "root" key in brackets. Thats used in rails to nest a param key in a hash eg. user[name].
I am using a form_for helper to create a new comment on a page. Whenever the submit button is clicked, I also want to give the new comment an attribute, like :this_id => 4. Is this somehow possible in the view?
Use hidden_field :
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field
hidden_field(:signup, :pass_confirm)
# => <input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{#signup.pass_confirm}" />
hidden_field(:post, :tag_list)
# => <input type="hidden" id="post_tag_list" name="post[tag_list]" value="#{#post.tag_list}" />
hidden_field(:user, :token)
# => <input type="hidden" id="user_token" name="user[token]" value="#{#user.token}" />
I am using Sinatra but I am guessing this also applies to Rails (if not, please remove the tag or let me know and I will remove it).
I have a ActiveRecord::Base class User. It has tons of attributes and I am displaying a page that will allow someone to update the a particular user. Problem is, I have a hard time implementing the update functionality in a DRY manner. What I mean is, when I get the params with a POST request, I can do:
a_user.update_attributes params
because params contains bnch of other crap too (like :splat - what's that?) and it will throw an unknown attribute error. What I instead have to do is this:
a_user.update_attributes {:attrA => params[:attrA],
:attrB => params[:attrB], ...etc }
(keep in mind there are A LOT of attributes)
Is this how I should do this? To me, for some reason...it doesn't feel right. If for example, I have another Model that needs to be updated in a similar manner, I have to rewrite manually all attributes again.
What I am looking for is somethign like:
a_user.filter_and_update_attributes params
where filter_and_update_attributes automatically filters params of any bad/unknown attributes and I can use this anywhere with any models with have to rewrite so much useless code.
Thanks
If you structure your form like this:
<form action="/users" method="post">
<input id="user_email" name="user[email]" type="text">
<input id="user_name" name="user[name]" type="text">
<input id="user_phone_number" name="user[phone_number]" type="text">
...
<input id="user_email" name="user[email]" type="text">
<input name="commit" type="submit" value="Submit">
</form>
you should be able to use the params like this:
#user.update_attributes params[:user]
When you name your html fields like user[email], the params hash looks like:
{ user: { email: "example#example.com", name: "Example" } }
So using params[:user] gets you that nested hash of parameters that belong to the user.
You can filter a hash using select. Find a list of all attributes of your model and test if the keys are in that list:
attrs = a_user.attributes.keys - User.protected_attributes.to_a
a_user.update_attributes params.select {|k,v| attrs.include?(k)}
i created a simple search input form for a Rails application, but i noticed that when i dont input anything it returns a result of all my users, but i dont want it to work when no value is placed
My form
<h6>Search for Friends</h6>
<form action ="users/search" method="post">
<input name = "key" type="input" />
<input value="Search" type="submit"/>
</form>
Controller method
def search
#users = User.find(:all,:order => 'username', :conditions => ["username LIKE ?", "%#{params[:key]}%"])
end
Try the new Arel writing or check if the key was assigned.
def search
#users = User.where(["username LIKE ?", "%#{params[:key]}%"]).order('username')
end
or
def search
if params[:key].blank?
#users = []
else
#users = User.where(["username LIKE ?", "%#{params[:key]}%"]).order('username')
end
end
I have some checkboxes on one view, that I want to eval on another, but I dont understand what its doing.
I've read posts/blogs stating different approaches to the name :-
update_params[] # array
update_params[0], update_params[1] # known indexed array
update_params0, update_params1 # differently named
So going with the first which seems the most common :-
# views/index.erb
<input type="checkbox" name="update_params[]" value="Copy" />Update the host<br/>
<input type="checkbox" name="update_params[]" value="Start" />Start the software<br/>
Should the value be indexed? 0, 1?
So I want two outcomes
1) Display the options selected from the index.erb view on the version.erb view.
So that it looks something like :-
Copy : Yes
Start : No
Currently I have :-
# views/version.erb
<p>Copy : <%= params['update_params[0]'] %></p>
<p>Start : <%= params['update_params[1]'] %></p>
2) eval the options to pass to a script so that they become command line options, ie -c, -l
So my "controller"
# update.rb
helpers do
def run_update(version, host, params)
command = "./update.sh #{params} #{host}" # -c -l
#ok = system( command )
end
end
post '/version' do
run_update(params[:version_list], params[:host], params[:update_params])
erb :version
end
Maybe instead of relying on index, you could use keys in the array? For example:
<input type="checkbox" name="update_params[copy]" value="Copy" />Update the host<br/>
<input type="checkbox" name="update_params[start]" value="Start" />Start the software<br/>
So then, you can do stuff like:
params[:update_params][:copy]