below is my code
<% if #user.empty? == true %>
<p> Sorry no data to display</p>
<%else%>
<% #user.each do |n|%>
User id = <%=n.id%> <br \>
User type = <%=n.type%> <br \>
User name = <%= n.name%> <br \>
<%= link_to "Good. You can proceed on creating a new", new_user_product_path(current_user)%>
<%end%>
<%end%>
This code is currently under searches#index. Now as you can see, after a set of results is shown, I want the user to be able to create a new product. But when creating the product, I want to make sure <%= n.name%> is pass over to new_user_product_path(current_user) (its a form). But not via url.
The form field which i want to populate is
<%= f.hidden_field :user_name%>
So, how do I do it?
Thanks
I think i found a solution
I just made <%=n.name%> to <% $name = n.name %> and since a global variable, its accessible now.
You don't need to pass name from search index, as you have current user reference:
<%= f.hidden_field :user_name, current_user.name %>
This will render a hidden input with a value of current user name.
Even you don't need this parameter to be passed after user submits form.
If you are doing this I suppose is to have the user name reference available in client, to do something like this in Javascript:
alert($('[ name = user_name ]').val());
Related
I have a form where users can add an item to their stock. I would like the form to use datalist. My code is this:
<div class="form-group">
<%= form.label :item_id %>
<%= form.text_field :item_id, list: 'item_id' %>
<datalist id="item_id">
<%= options_for_select(current_user.items.map { | item | [item.name, item.id] }) %>
</datalist>
</div>
But when the user type their search and click on the autocompleted name (for example 'Apple'), the form then displays the id of the item (for example '7'). I would like that instead of changing it to the id, the form only displays the name. How can I achieve that?
This is not a good idea - at all. Unlike a select a text input is not label/value. What you see is what you get.
So while you could do:
<datalist id="item_id">
<% current_user.items.each do |i| >
<option><%= i.name %></option>
<% end %>
</datalist>
Which will fill the item_id input with the name. But you'll lose the id on the way. Which means that you would either need to lookup the item by name or use some sort of javascript trickery to hamster away that id and replace the input value before the user submits it.
At which point you could actually create a AJAX autocomplete with the same amount of effort thats less of a hacky mess.
According to this rails guide it is acceptable to key params with the name you associate with the html values in your template,
"When the form is submitted, the name will be passed along with the form data, and will make its way to the params hash in the controller with the value entered by the user for that field. For example, if the form contains <%= text_field_tag(:query) %>, then you would be able to get the value of this field in the controller with params[:query]"
However, when I attempt to access a param I submitted it throws the following error:
I understand that there are other params besides :name but I am just trying to get this one handled for now before passing the others. I am just making this app to practice the fundamentals.
Also, how come my params are being passed in my URL? These are forms, shouldn't they be submitted as a POST request and thereby able to use strong params with? I think the reason i'm having so much trouble is because I am used to strong params, I am unfamiliar with this syntax. Any help is appreciated, thanks!
Here is my code:
Controller:
class OrdersController < ApplicationController
def create
Order.create(params[:name])
end
end
View
<h1> Input information </h1>
<%= form_tag("/order",method: "get") %>
<%= label_tag(:name, "Enter name") %>
<%= text_field_tag(:name) %>
<%= label_tag(:email, "enter email") %>
<%= text_field_tag(:email) %>
<h2> Select color </h2>
<%= label_tag(:red,"red") %>
<%= check_box_tag(:red) %>
<%= label_tag(:green,"green") %>
<%= check_box_tag(:green) %>
<%= label_tag(:indigo,"Indigo") %>
<%= check_box_tag(:indigo) %>
<%= submit_tag("submit") %>
You need to provide a hash argument to create. How will Order know that you are setting name? You are basically doing this:
Order.create('phoot')
You need to specify what phoot is:
Order.create(name: params[:name])
# ends up being Order.create(name: 'phoot')
In my rails app, if a user mentions another username in a comment by using the # character, such as #max i'm trying to add autocomplete to suggest a list of users and then automatically create a link_to (username, user_path(user)
This is what I have in my comment partial:
<%= form_for [commentable, Comment.new] do |f| %>
<%= hidden_field_tag :commentable_type, commentable.class.to_s %>
<%= hidden_field_tag :commentable_id, commentable.id %>
<p>
<%= f.text_area :body %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
I'm trying to use this gem: https://github.com/ichord/jquery-atwho-rails
It says to bind the text area with
data = ['tom','john'];
$('textarea').atwho({at:"#", 'data':data});
Where do I actually put this? Can I do something like data = User.all? Should I just be using a regular expression to do this?
I think the reason that data = User.all isn't working is because User.all will return an array of User objects. What you want to do is retrieve those User object usernames (or whatever you want the autocomplete to use, and store that in data instead.
You might try something like
#usernames = User.pluck(:username)
to get all the usernames. Then, in your partial:
data = <% #usernames &>
$('textarea').atwho({at:"#", 'data':data});
This is assuming of course that your partial is an .erb file where you can embed ruby code.
You can do something like this:
<script>
data = <%= raw User.pluck(:username).compact.to_json %>;
$('textarea').atwho({at:"#", 'data':data});
</script>
You might want to move the loading of the usernames into the controller or a helper method. The whole sniplet might belong into an view partial to keep things organized. And it might not be the best idea to load all usernames into the view when there are too many users in the database.
I want my app to let the user choose between a few addresses or create one. The address is to be stored inside the contract object (Contract.adresse_id).
The controller generating the page displaying the form generate a #user variable.
The goal is to either pass the id of an existing address to the next controller, or to pass the params of a new adress (classic nested). This page only goal is to set the adress, no other fields of Contract should be modified.
The model Contract has a address_id field. I want a radio button allowing to choose between :
#user.address # it is an address id
#user.secondary_adress # address id too
and an empty form allowing to create your own address.(classic nested)
i guess it should look like :
<%= form_for contract do |f|%>
<% f.label :address%>
<% end%>
But then i do not know what to do. How can i do a form allowing to chose between 3 addresses where 2 already exists and the third is to be created ?
You could make your third radio option an 'id' of 0 or -1. Then in the next controller when you look up the Address, you instead make a new Address if the id is your fake non-id. You could probably use the same view in either case on that next controller by setting the url of that form based on whether the Address is new. eg:
- if #address.new_record?
- form_url = [create path]
- else
- form_url = [update path]
...
= form_for(#address, :url => form_url) do |f|
(That's in haml instead of erb, obviously. I highly recommend making the switch)
My problem is in 2 part :
1) choose an existing address to put it in contrat
2) add the possibility to create a new address instead of the existing ones in the same page
For 1) i did this :
<%= form_for #contract ,:action => "confirm" do |f| %>
<div><%= f.radio_button 'address',#user.address.id, :checked => true %>
<%= render #user.address %>
</div>
<div><%= f.radio_button 'address',#user.address_secondary.id, :checked => true %>
<%= render #user.address %>
</div>
<%= f.
<%= f.submit %>
<% end %>
It simply submit the id of the chosen existing address
2) Instead of making a complicated single form allowing to chose between the two, i did a second form with a nested new address. It imply i create a new address in the previous controller, but if i do not use it, it's not a problem.
In the end my page have two different form submitting different data but it is way easier to do.
I'm using a submit_tag form helper in one of my apps. The value of this submit button should change dynamically. The two possible values for this submit button are Save and Update. So, in the view, I have done something like the following:
<% temp = 0 %>
<% text = '' %>
<% temp = ActivityLog.find_by_sql("SELECT COUNT(id) AS cnt FROM logs WHERE id > 0")%>
<% text = temp[0][:count].to_i > 0 ? 'Update' : 'Save' %>
<!-- other html contents -->
<%= submit_tag text, :id=>"submitBtn"+i.to_s, :onclick=>"submit_button_clicked(this)"%>
Now, when I run the view inside a browser, I can see the desired effect. But the rails controller receives the erroneous value for the commit options in the params hash.
For instance, when the value of text is evaluated to Save, I get the following in the Firebug:
<input type="submit" value="Save" style="" onclick="submit_button_clicked(this)" name="commit" id="submitBtn3">
But raise params.inspect in the associated controller shows the follwing:
{"commit"=>"Update",
"authenticity_token"=>"",
"time"=>{"292"=>"3.0",
"2"=>"1.0",
"456"=>"4.0"},
"date"=>"2011-09-20"}
See, although the value of the Submit button is shown as Save in the HTML, the rails controller shows the value of commit as Update. What's wrong in here?
If you are using Rails helpers, it provides a simple way to choose text on button with according to type of form:
<%= form_for #activity do |f| %>
<%= f.label :title %>:
<%= f.text_field :title %><br />
<%= f.submit %>
<% end %>
When no value is given, it checks if the object is a new resource or not to create the proper label. In the example above, if #activity is a new record, it will use "Create Activity" as submit button label, otherwise, it uses "Update Activity".
P.S. please do not use SQL in your views