if I have a manually created html form with the following input field
<input type="text" name="email">
it gives the following params {"email" => "123" }
How can I wrap it with message_fields without specifying it inside the form?
eg without the following
<input type="text" name="message_fields[:email]">
So I just want to wrap my params with message_fields from my controller.
controller strong params
params.permit(:comment, message_fields: {})
The idea is to assign all incoming params to message_fields
which is t.hstore "message_fields"
"message_fields"=>{":email"=>"qew"}"
had to do the following but there definitely should be something better
fields = #mailbox.allowed_fields.gsub(" ", "").split(",")
h = {}
fields.each do |f|
h[f] = params[f]
end
attrs = message_params.tap do |p|
p["message_fields"] = h
end
then I just pass attr to create meth insted of message_params.
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 quite new to Ruby, but have some experience with programming. I am trying to figure out how to pass a variable collected in a form (SEARCH) using POST, to its controller (API) and then output it into another view (RESULT) that is also in the same controller.
I know the variable makes it to the API controller, because it shows up in the server log. But I can't figure out why it won't pass from there as an instance variable to the Result.html.erb page.
Routes.rb
Remixr::Application.routes.draw do
get "api/search"
post "api/result"
end
search.html.erb
<form action = "result" method="post">
Zip Code: <input type = "text" size = "5" name = "zip_code" />
Search Range: <input type = "text" size = "3" name = "range" />
<input type = "submit" value="Search" />
<input type = "reset" value="Reset Form" />
</form>
result.html.erb
Zipcode = <%= #zip_code %>
Radius = <%= #range %>
api_controller.rb
def search
end
def result
#zip_code = zip_code
#range = range
end
end
I know this some rudimentary stuff here, but I can't find anyone that shows a form used in one view , POST the form contents to its own controller to use another method in that controller and then output variable made in that method to another view under the same controller. I cut out a lot of the other processing that is going on until I am sure that I can pass variables in the fashion I laid out.
In the controller, use:
def result
#zip_code = params[:zip_code]
#range = params[:range]
end
I have a form where i generate particular set of user input fields based on how many times the user want to generate
example:
If the user give 2 as input then a piece of javascript will generate tow set of same input fields
Workflow example:
How many users: 2
//Generated fields
Enter user 1 information
Name:
Address:
Photo:
Enter user 2 information
Name:
Address:
Photo:
I have set the field names as array
<input type="text" name="name[]" >
<input type="text" name="address[]" >
<input type="file" name="photo[]" >
<input type="hidden" name="user_count" value="count">
In my controller when the loop reaches the photo field it throws error undefined method [] for nil:NilClass
#current_user = 0
#number_of_users = params[:user_count].to_i
until #curret_field > (#number_of_users-1) do
#user=User.new
#user.name=params[:name][#current_user]
#user.address=params[:address][#current_user]
#user.photo=params[:photo][#current_user]
end
You can use the .try method to test which param is missing:
params[:name].try(:[], #current_user) # returns nil if can't access to it
But I would change my params keys/values mapping to the following:
#users.each_with_index do |user, i|
text_field_tag "users[#{i}]name"
text_field_tag "users[#{i}]address"
file_field_tag "users[#{i}]photo"
end
And loop in the controller like this:
params[:users].each do |user_params|
User.new(user_params)
end
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 created model, controller and view with rails scaffold generator:
rails g scaffold Todo description:string tags:array
So I have the model:
class Todo
include Mongoid::Document
field :description, :type => String
field :tags, :type => Array
end
And controller:
def create
#todo = Todo.new(params[:todo])
#todo.save
But this case (auto-generated code) I get error that tells me something like:
tags field must be array datatype, but you're trying to use string
So I have fixed the controller:
def create
##todo = Todo.new(params[:todo])
#tmp = params[:todo]
#tmp["tags"] = #tmp["tags"].split(',')
#todo = Todo.new(#tmp)
And I'm just wondering if there is any better way to fix my error?
Depends on how your view is structured. From what I see, there must be a single text input or something, into which you input tags, separated by comma. No wonder it comes as a string! In this case your workaround is correct. I would add stripping of leading and trailing whitespace, though.
#tmp["tags"] = #tmp["tags"].split(',').map(&:strip)
To get a real array in params your HTML must look like this:
<input type='text' name='tags[]' />
<input type='text' name='tags[]' />
<input type='text' name='tags[]' />
Where each of these inputs holds a single tag.