The problem is this: I am showing some data to the user in a table and in it has a field of the numeric type that the user fills. However, I do not know how to send the value populated by the user as a parameter to the controller.
I've tried using the following syntax:
<td><%= number_field :stock, params[:amount] %></td> ## HERE IS THE PROBLEM
<td><%= link_to 'Buy', new_transaction_path(stock: {stock_id: "#{stock[:id]}", buy_price: "#{stock[:value]}", amount: "#{params[:amount]}", transaction_type: 'buy'}), method: :post %></td>
I would like to pass the value that the user informed thought the number_field as a parameter to the controller, but it's not working..
For your case "that the user fills", You have to user a form with a submit button, Here is a relate question, If the value is determined you can use link_to otherwise a form is better, Although sometimes I rely on JS or Jquery, I give the link_to a specific id and I give the input field and id too (HTML id), Then make an action listener on the the <a> that link_to generates, Once user click on it I get the data and submit the data.
The problem for the JS way is that if you are listing a table for more than one link_to for the product with multiple numeric_field you have to make sure that the id is unique, you can simply doing this by adding _#{id} part to the generated id.
Related
I'm trying to build a form that allows a user to add products to an order.
The way I have it setup so far is that a user will select from 2 dropdown boxes and type into 1 text field
1 - the product they want
2 - its size
3 - the quantity they want.
What I hope to do is have the user click a link_to tag to "Add" this item to their order.
I was thinking I could do this via ajax and build the associative record in my controller and have it render on the page when the request returns.
When the user is done with their order and hits submit I can create my Customer Order with the products they wish to buy.
Am I approaching this correctly?
e.g. my form has the following:
<%= collection_select :order_line_item, :cake_id, Cake.order(:name), :id, :<%= grouped_collection_select :order_line_item, :cake_size_id, Cake.all, :cake_sizes, :name, :id, :name %>
<%= label_tag :quantity %>
<%= text_field_tag :quantity %>
<%= link_to "Add to order", add_to_order_path, {method: :post, remote: true} %>
Am I approaching this correctly? I then need to be able to add the fields above to the ajax post so I can populate the associative record with the relevant values.
Am I approaching this correctly?
I don't know about 'correctly'. But, I can imagine some alternatives.
Here are some sketches:
One Option:
This approach assumes that the Order is already saved so that you can associate a Product with that order. Perhaps Order has a status.
You could wrap that whole bit (product, size, quantity) in its own form (not embedded within your order form).
Have the form submit via js using remote: true (if you're using Rails 5, then this may be the default behavior).
When the user clicks on "Add", you will receive the field values as parameters in your controller where you can associate the Product with the Order.
Then, render back an HTML blob that can be inserted into the DOM (perhaps an order row?)
Use js to insert the blob and clear the form.
Another Option:
You could leave that whole bit (product, size, quantity) as not a form and have it reside outside your form.
Wrap it all up in a div.
Convert that link into a span or something similar.
Attach an .on 'click' event (I'm assuming jquery, you don't specify, so I'm going to run with it) to the wrapper.
When the link is clicked, the click event will bubble up to the wrapper.
Have the wrapper submit the field values via ajax.
Proceed as above.
I wouldn't really recommend this approach as it seems to me that you're basically replicating the functionality of a remote form. But, there is...
Yet Another Option
This approach does not require that the Order already exists.
You could have a hidden order item row outside of your form.
You construct your page as above in Another Option.
Now, when the user clicks the "Add" button, clone the hidden order item row.
Fill in the cloned order item with the appropriate values.
Insert the cloned order item into your Order form.
When the user clicks "Order" or "Submit" or whatever they click when they're done, you'll get all of the order rows as field sets.
Process the order line items along with the form. (Some folks might suggest accepts_nested_attributes_for, but I never use that.)
I suspect there are others. Or perhaps variations.
I am not sure what the correct approach is for my situation:
I want to create a link_to pushing all checkboxes with value="1" into an array, or individually if array is not possible, but I am at a loss of how to express that?
<% #cards.each do |card| %>
<%= check_box("#{card.name}", card.id, {checked: true}) %><%= "#{card.name}" %>
<% end %>
(Rails 4.2)
After a long time of dead ends, trying to make it a 'clean' solution I ended up with this very dirty approach. But as they say, done is better than perfect:
Create a link_to that would include all the cards, but add one additional params: user_selected_cards = "".
Create a javascript that listens to for checking/unchecking of the checkboxes and reads the id associated with that specific checkbox. Then take that incoming info and add or remove it to the actual url that the link_to generates by finding the user_seletected_cards= portion in the url and add or remove the id depending on if isn't or already is added to the list after the equal sign.
I'm a RoR beginner and am using Rails 3.2.3.
I have a search form on my page and it performs a get request and filters the results correctly.
I want to add the possibility of also searching Products by dates.
I inserted a date_select and am able select the date and when the page refreshes after the search, the chosen dates are still there on the date_select, as I am able to get them through params.
However, my issue is that when the page renders the products, they have a link_to to their show action.
My goal is to also pass alongside the url the dates that were selected to perform the search on that link_to.
For ex, if the user selects a date of 20-06-2012 to 25-06-2012 it only shows products inserted on that time frame (and all those params are on the url)
But the link to show action of each displayed product is only:
link_to <%= link_to product.name, product%>
which renders
http://localhost:3000/products/24 (por example)
what I want to render/show is something like:
http://localhost:3000/products/24?from=20-06-2012&to=25-06-2012
The selected dates to perform the search are not stored in the database at this moment, but I will need to get them from the URL on a latter page, therefore, both dates will need to be preserved through 2 different pages before the users fills a form and then those dates are inserted in the DB.
Any tips on this? I've searched but all I found was on how to pass variables that exist in the model and I do not want to use cookies nor session variables.
Thanks in advance,
Regards
If you want to pass arbitrary parameters to the following call
<%= link_to product.name, product %>
you need to invoke the path method explicitly instead of using the implicit version. The line above is equivalent to
<%= link_to product.name, product_path(product) %>
Then you can pass parameters
<%= link_to product.name, product_path(product, :from => 'whatever', :to => 'whatever') %>
Can you try like this
link_to <%= link_to product.name, product, :param_you_want => "value you want" %>
Now you can set value in "param_you_want" as you wish.
But in my case set value in it "value you want"
Hope you can get idea
I'm about to finish the final part of my thread messaging system for users. All deletion works great however before I move on to my next feature I'd like to give users the ability to delete selected messages.
Here's a way I've thought of doing it so far.
Add a check box tag to the each loop that loops through each message.
Have a "delete selected" link that goes to my messages controller "destroy_selected_messages" action.
What I need to do is some how grab an array of all the selected messages id's. Then pass it to the path as an argument. The delete all links path.
<%= link_to 'Delete Selected', messages_destroy_selected_messages_path(ARRAY_WITH_IDS), :method => :delete, :confirm => "Are you sure?" if #current_thread_messages.any? %>
This delete selected link won't be part of the loop because I don't want it showing for every message but at the top of the thread instead.
I need to figure out how to pass the array with all the selected messages ideas into that argument. How do I get them from the each loop without going into my messages helper and writing some funky method.?
I have the checkbox tag e.g. check_box_tag ... how do I setup an empty array and then so I can pass in the messages id? e.g.:
<%= check_box_tag ......., :value => message.id &>
Help would be appreciated. I looked at an old screencast in railscasts but it's from 2007 I think.
Kind regards
You can make name="message_ids[]" for multi select inputs. It will get passed as an array through HTTP server to your params[:message_ids].
From the HTML side of the problem, I think that form helper <%= check_box_tag "message_ids[]", :value => message.id %> should suffice.
In the controller action, log the params[:message_ids] and look it up, it should be an Array.
I have a has_many_and_belongs_to_many relationship between items and builds.
Whatever I do, I just cannot get the name of the items to be displayed for How do I output name instead of ID? and the rest. It just displays the ID :/.
In the code provided below, the ID of the items are being displayed instead of the name of the item with that ID. How do I display the name of the item?
<% current_user.builds.each do |build| %>
<tr>
<td><%= build.hero.name%></td>
<td><%= build.user.email%></td>
<td><%= build.name %></td>
<td><%= build.starting_items %></td>
<td><%= build.early_items %></td>
<td><%= build.core_items %></td>
<td><%= build.situational_items %></td>
</tr>
<% end %>
Here is the github repo: https://github.com/imjp/DotA-Items
Basically, what I want to do is to be able to enter the id of an item in my form that later on gets displayed as the item name.
I'm not even sure if the name starting_items is the best for this, or if i should just use item_id. Because I'm planning on adding a lot of different inputs where users can enter items that need to be displayed as names.
Do you think the name starting_items is good for one of the fields, or should I use item_id?
Isn't there a way I can display the name of an item with ruby by doing something like Item.name.find(build.starting_items) or something? Since the starting_items value is an item_id?
When you call build.starting_items which I assume is a named scope or method call you are probably returning an array of the items themselves.
Following this it's likely that each of the items if being inspected or you are seeing the output of item.to_s.
In order to display the names of these items instead of the object id you probably wish to use something like:
<td><%= build.starting_items.map(&:name).join(', ') %></td>
This will call map on the array and pass out each of the names and then join them (you could also use .to_sentence in Rails).
Looking at the code you are retrieving a list of all items from the database and using them to fill a semantic form. If you take a look at the output of the items in the html it is creating a list of options with the item id as the value of the select.
When the form is saved it saves a string containing the single item ID in the database.
Firstly if you need to accept multiple items you could specify a number of has and belongs to many relationships such as:
has_and_belongs_to_many :starting_items, :join_table => "items", :foreign_key => "item_id"
You then need to make sure that the form actually passed the item id's along to ensure that all of the id's are saved correctly.
Ok from re-reading what you've written in response to the other answer, I now guess that the column that you're having trouble with is:
<td><%= build.starting_items %></td>
(let me know if that guess is wrong and tell me the right one).
Rails doesn't automatically know that you want to see the names of these items. It will give you exactly what you've asked for - and here you have asked for the set of actual items... not the names of the set of items. To turn these item-objects into a list of the names of the items, you must call the "name" method on each item. you can do that using the code the other answer gave:
<td><%= build.starting_items.map(&:name).join(', ') %></td>
UPDATE
ok, reading through your codebase.
The problem is that you aren't actually saving the starting_items as actual items. In the form where you create a "build", you have the starting_items field as a select-box... the select box stores a set of ids. and id is just an integer value.
nowhere in your code do you turn that set of integer id values into actual Item objects.
So... when you go to look at that list once more... it's still just a set of integers.
To see the names of the Items that have those ids, you will need to actually instantiate those Item objects... and then call the 'name' method on them eg:
<td><%= Item.find(build.starting_items).map(&:name).join(', ') %></td>
Note: you'll also need to add basic checks eg that build.starting_items is not empty, and that the ids are in fact valid ids...
I seem to have figured this on my own after buying a Ruby book.
First, I created a new helper method in application.rb called starting_item which goes as follows:
def starting_item(id="6")
#item = Item.find(id)
"#{#item.name}"
end
Why helper method? This way I can call this method from within my views.
Now, to display the name of the item in my index view, I do just call the method with the argument as follows: <%= starting_item(build.starting_items) %>