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
Related
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.
Part of the app i'm developing involves a form which has to create a product on the users shopify store and also create a row in my own database for an identical product (with a few extra bits of information).
Where i'm struggling is with the form itself, i can do this with a html based form, but i can't get a single ruby form to do both jobs. A shortened version of my controller create code is as follows;
def create
#item = Item.new
#item.item_title = params[:item_title]
#item.item_profit = params[:item_profit]
#new_product = ShopifyAPI::Product.new
#new_product.title = params[:item_title]
#new_product.save
#item.save
end
So as you can see i'm using the same params to set values for both the shopify product and the product in my own db. The HTML form looks like this:
<form action="/items/submit" >
<input type="text" name="item_title">
<br>
<input type="text" name="item_profit">
<br>
<br>
<input type="submit"/>
</form>
And it works fine, but how do i convert this into a ruby form that does the same job?
You can do like this:
form_tag('/items/submit')
text_field_tag 'item_title'
tag("br")
text_field_tag 'item_profit'
tag("br")
tag("br")
<%= submit_tag 'Save' %>
Instead of tag("br"), you can directly use < br />.
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].
In my application, I have search button and based on the search criteria, I have to search data. I have used few hidden variables to store data.
My .gsp page looks like this:
<g:form id="job" name="job" method="POST">
<div>
<input type="hidden" id="country" name="country" value="${country}"/>
<input type="hidden" id="paginationFlag" name="paginationFlag" value="${paginationFlag}"/>
<!-- There are other components like text field, select box etc -->
<g:actionSubmit id="Search" value="Search" formmethod="post" action="findJob"/>
</div>
</g:form>
My respective controller method looks like this:
def findJob(){
def country
def paginationFlag
if(params?.country){
country = params?.country
}else{
country = 'USA'
}
if(params?.paginationFlag){
paginationFlag = params?.paginationFlag
}else{
paginationFlag = 'false'
}
withFormat{
html{
List<Applicant> searchList //get data from database.
// other business logic
render(view : "jobList",model:[paginationFlag: paginationFlag, country:country])
}
json{
// some business logic
def candidateList // value for this candidateList is acquired from database
def json = ['jsn': candidateList]
render json as JSON
}
}
When I click search button and debug the code, first my control goes to controller-> findJob() method and executes the code inside html part.
Secondly, it goes to the gsp page (view page) and sets value.Third, it again goes to the controller and executes the code inside json part.
On the first entry to the controller, the value of paginationFlag and country in param are both null. So it will set the value 'false' and 'USA' respectively. But when control goes to controller again for the second time, again the value of param.paginationFlag and params.country are null. Should not they have the assigned values?
Why is it so? What should I do go get the value of country and paginationFlag in params on the second time? Can any one explain me ? Thank you very much for advance.
The problem is you're using a regular HTML input tag rather than a Grails g:hiddenField tag. Try this:
<g:form id="job" name="job" method="POST">
<div>
<g:hiddenField name="country" value="${country}"/>
<g:hiddenField name="paginationFlag" value="${paginationFlag}"/>
<!-- There are other components like text field, select box etc -->
<g:actionSubmit id="Search" value="Search" formmethod="post" action="findJob"/>
</div>
</g:form>
Tip
You can also simply the params assignment:
def country = params.country ?: 'USA'
def paginationFlag = params.paginationFlag ?: 'false'
I can't tell where the paginationFlag comes from, but know that a boolean is valid.
I'm wondering how I can populate a text box in my view from a list in my controller, I have been searching for examples for awhile, but haven't really found anything. I'm uncertain on how to access the text field from the view exactly, where as in Java you could do something as simple as jTextField.getText(). I'm also wondering on how to grab the text in my text box as well.
Below I'll post some example code of what I'm doing.
Controller:
def loadFile = {
def propFile = "c:/props.txt"
def propMap = [:]
def propList = []
new File(propFile).eachLine { line ->
def (key,value) = line.split(':').collect { it.trim() }
propMap."$key" = "$value"
if(propMap) {
propList << propMap
propMap = [:]
}
}
}
def saveFile = {
//get contents of text box
//over-write props.txt with new information
}
View:
<g:textField name="properties"/>
<span class="menuButton"/><g:link action="loadFile" controller="myController">Load File</g:link>
<span class="menuButton"/><g:link action="saveFile" controller="myController">Save File</g:link>
So, my question seems relatively straight forward, how do I access the text box when I want to populate it and save the data from it?
.
.
.
EDIT
After checking out some of the examples submitted by you guys, I have one last question.
Why does the following code act different when clicking the Load File button?
<g:form controller="drive">
<g:textArea name="properties" value="${params.param1}" rows="50" cols="6"/>
<br/>
<g:submitButton name="loadFile" value="Load File"/>
<g:submitButton name="saveFile" value="Save File"/>
</g:form>
<span class="menuButton"/><g:link action="loadFile" controller="drive">Load File</g:link>
When clicking the g:submitButton dealing with loadFile it redirects me to the list gsp. However, when I click the menuButton it loads the textArea with the text from the file. The reason I ask is because with the second option, the button isn't located where I would like it to be.
Wrap your text field in form tag. This way, when you submit it, you can access your field in controller.
<g:form controller="myController" action="saveFile">
<g:textField name="properties"/>
<g:submitButton name="saveFile" value="Save File" />
</g:form>
Then, you can access your properties field in controller:
def saveFile = {
def properties = params.properties
// do whatever you need
}
EDIT:
To address some of the issues that came up later in comments, I'll try to provide some more insight.
Let's assume that your controller is called Drive and view (the form to submit properties) is drive/properties.gsp. If want your load button to load your properties into the text area, you can do something like this:
def loadFile = {
// your code here
render(view: 'properties.gsp', model=[properties:propList])
}
And in your view:
<g:form controller="drive">
<g:textArea name="properties" value="${properties?.join("\n")}" rows="50" cols="6"/>
<br/>
<g:actionSubmit name="loadFile" action="loadFile" value="Load File"/>
<g:actionSubmit name="saveFile" action="saveFile" value="Save File"/>
</g:form>
This should render your form with values from your file, each property in new line. But I didn't test it.
jjczopek's answer shows how to get access to the data in the controller after it has been submitted from the view.
You can pass a default value through from the controller to the view by setting a parameter in the controller eg...
params.param1 ='value to pass'
render(view:"testView")
Then in your view you can retrieve it again with...
<g:textField name="text1" value="${params.param1}"/>
You may also find this question and these docs useful.
If want to pass domain objects through then one of the following specific render methods might be better...
Grails Controller - Render