I'm trying to build two forms, nested. But all the resources I've found so far deal with how to save the main object, maybe add or remove nested objects, but not saving the entire list elsewhere with the values of the form. Is there a way to do this?
Here're the model I'm working with. I'm trying to create a pc configurator which will allow you to choose different components and quantities, and then you would add to the basket.
So I'm working with three models. Configuration, Configoption, Item.
-- Configuration model
has_many :configoptions
-- Configoption model
belongs_to :configuration
has_many :items
def range
a = []
b = self.quantity_rng.scan(/\w/)
b.each do |p|
a << p.to_i
end
return a
end
-- Item model
belongs_to :configoption
scope :sorted, order('items.position ASC')
The idea is that every configuration has a set of options, say 5. For each of these 5 options there could be a number of available items. For example option 1 is the processor, 2 could be RAM and so on.
-- Configuration controller
def list
#configurations = Configuration.find(:all)
end
def show
#configuration = Configuration.find(params[:id])
#options = #configuration.configoptions
end
-- configuration/show view
<table>
<tr>
<th>Elements</th>
<th>Quantity</th>
</tr>
<%= form_for(:configuration, :remote => true) do |p| %>
<% #options.each do |option| %>
<tr>
<%= form_for(:option, :remote => true) do |f| %>
<% if option.items.count > 1 %>
<th id="<%= option.name %>"> <%= f.select option.name, options_from_collection_for_select(option.items.sorted, 'id', 'name')%></th>
<% else%>
<th id="<%= option.name %>"> <%= f.label(option.items[0].name) %></th>
<% end %>
<% if option.quantity_rng == nil %>
<th id="<%= option.name + "_qty" %>"> <%= f.label(option.quantity) %></th>
<% else%>
<th id="<%= option.name %>"> <%= f.select option.name, option.range, :selected => option.quantity%></th>
<% end %>
<% end %>
<% end %>
</tr>
<% end %>
</table>
So far things are actually good, I can give different items to the options and the .range method let's me say quantity_rng in an option "1,2,3" and will make it into the array [1,2,3] passed for a drop-down if needed.
But now comes the crucial part, actually adding what I selected to the basket. How do I capture what the user has manipulated in the drop downs and store it (not as changes to the configoptions themselves, but as a new object elsewhere?)
BTW I'm using remote => true, because I intend to put some validation rules from jQuery later on, but one step at a time ;)
Thanks a lot!
May be i dint understand well your idea but i thing you need to use p.fields_for as nested options. Look at http://apidock.com/rails/v3.0.0/ActionView/Helpers/FormHelper/fields_for and dot forget to define
def address_attributes=(attributes)
# Process the attributes hash
end
in your model Configuration
Related
I would like to make a smooth streamlined process for placing customer orders.
I feel the best tool for the job is a wizard - unless there are other suggestions?
I need to pass arguments to next steps in my wizard.
My controller looks like this:
include Wicked::Wizard
steps :item, :customization, :upsell
def show
# #order_session_id = params[:order_session_id]
#order = Order.new
#order.order_session_id = params[:order_session_id]
case step
when :item
#items = Item.all # change with group...
when :customization
# item = Item.find(#order.item_id)
# item = Item.find(params[:item_id])
item = Item.find(1)
#customizations = item.customizations.split("|")
when :upsell
#upsells = Upsell.find_all_by_group_id(item.group_id)
end
render_wizard
end
def update
#order_session_id = params[:order_session_id]
#order.attributes = params[:order]
render_wizard #order
end
The view for item looks like this:
<%= form_for #order, url: wizard_path(:customization), :method => :get do |f| %>
<table border="1">
<% #items.each do |item| %>
<tr>
<td class="item_selection" onclick="$(this).children('input').attr('checked', 'checked');return false;">
<%= f.label :item_id, "#{item.name}" %>
<%= f.radio_button(:item_id, item.id) %>
</td>
</tr>
<% end %>
</table>
<div class="actions">
<%= f.submit "Continute" %>
</div>
<% end %>
I need to pass the selected item_id to the customization step so that the next part of the wizard can work.
How can I do this?
Thanks
You can pass parameters in the url after the step parameter.
wizard_path(:step_1, item: item)
i am getting stuck with using the gem ransack. I have placed ransack in my gemfile and then run bundle install ( though i used just bundle, does that make a difference? i didnt think it did?)
Next i have placed this in my Recipe controller
def all_recipes
#q = Recipe.search(params[:q])
#searchresults = #q.result(:distinct => true)
end
Within my view (all_recipes)i have the search form and block to display my results
<%= search_form_for #q do |f| %>
<%= f.label :dish_name_cont %>
<%= f.text_field :dish_name_cont %>
<%= f.submit %>
<% end %>
---------------------
<% #searchresults.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
</tr>
<% end %>
Ive got two problems, without even conducting a search i get this in my view where the block is
fish and chips my first recipe lasagne Lasagne
and then if i search say las it redirects me to my index page after the get request but as i have no block to display the results i get an undefined error, which is expected.
After this i placed my controller code within the index action and the form and block within the index view and now it all works? Why cant i use the all_recipes action and why does it redirect?
In depth demo, may be helpful - http://railscasts.com/episodes/370-ransack?view=asciicast
I am taking a rails class at my University and I am trying to create a search form which will show the results on the same page rather than show a different page of results. Is this something simple to do? I am creating a museum app with artifacts for each museum but I want the user to search artifacts from either page.
On my routes.rb I have
resources :artifacts do
collection do
get 'search'
end
end
On my museum index I have the code below that he gave us but not sure how to tweak the get routes for the same page.
<%= form_tag search_artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
<% if #artifacts %>
<p> <%= #artifacts.length %> matching artifacts. </p>
<h2> Matching Artifacts </h2>
<% #artifacts.each do |a| %>
<%= link_to "#{a.name} (#{a.year})", a %><br />
<% end %>
<% end %>
Yes, this is easy. Just have the index page return the search results if params[:search_text] is present - this way you don't need a new route or a different page.
class ArtifactsController < ApplicationController
def index
#artifacts = Artifact.search(params[:search_text])
end
end
class Artifact < ActiveRecord::Base
def self.search(query)
if query
where('name ILIKE ?', "%#{query}%")
else
all
end
end
end
So then your form looks like:
<%= form_tag artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
Edit:
So what you really want to do is any page you want to search, include a form which makes a request to that same page.
Then in each of those controller methods just put this line of code:
#artifacts = Artifact.search(params[:search_text])
and that will populate the #artifcats array with only artifacts that match the search query.
Try using "Ransack" gem. It can also perform some more powerful searches.
My view:
<h1><%= #territory.name %></h1>
<%= link_to 'List of Territories', territories_path %>
<%= render 'shared/address_form' %>
<table>
<tr>
<td><strong>Name</strong></td>
<td><strong>Street</strong></td>
<td><strong>District</strong></td>
<td><strong>Note</strong></td>
<tr>
<%= #addresses.each do |address| %>
<tr>
<td><%= address.name %></td>
<td><%= address.street %></td>
<td><%= address.district %></td>
<td><%= address.note %></td>
</tr>
<% end %>
</table>
The form I render here is:
<%= form_for [#territory, #new_address] do |f| %>
<div>
<p>
<%= f.label :address %><br />
<%= f.text_area :address %>
</p>
</div>
<div class='file-wrapper'>
<%= f.submit "Submit" %>
</div>
<% end %>
Here is the territories controller, where the instance variable addresses is defined:
class TerritoriesController < ApplicationController
def index
#territories = Territory.all
end
def show
#territory = Territory.find(params[:id])
#new_address = #territory.addresses.build
#addresses = #territory.addresses
end
.
.
.
Why is Rails displaying
#<Address:0x7e088224>#<Address:0x7e0881d4>#<Address:0x7e088134>#<Address:0x7e088094># <Address:0x7e087ff4>#<Address:0x7e087f54>#<Address:0x7e087eb4>#<Address:0x7e087e14>#<Address:0x7e087d74>#<Address:0x7e0bce48>
after the form and before the table?
Thanks
Thomas
Check your layouts (app/views/layouts/*). Most likely you have included some ERB code in the one that is being rendered with this page that displays these addresses. Is that the full code of your view?
Edit: I found your solution. Right now, you have <%= #addresses.each ... %>. The each method runs the block on all elements, and then returns the list of elements. You do not want this code to be displayed. Remove the = so that <%= is just <%
You have some view code somewhere (in a layout or a view helper) that is implicitly calling the to_s method of your Address model instances. Look for something like <%= #address %>.
As you have seen, the non-overridden behaviour of the to_s method is to output the memory address of the object instance.
Those are not memory addresses. Those are instances of your Address class. If you'd override the to_s method in that class, you'd see that output there instead. And the reason you see those object printed out is your use of <%=. Changing this line
<%= #addresses.each do |address| %>
to this
<% #addresses.each do |address| %>
should fix it.
First: I can see no form in your view.
Second: Your view looks ok.
Have a look at your layout files.
i have this in the controller
#ads = Ad.all(:joins => 'LEFT JOIN states ON ads.state_id = states.id')
but i have problem to query field of states table.
any idea?
<% #ads.each do |ad| %>
<tr>
<td><%= ad.title %></td> <- title is ad field.no problem
<td><%= ad.name %></td> <- name is states field.problem at here
</tr>
<% end %>
I don't think this will work unless you have associations set up. Unless performance is a concern, you may just want to use the association without joins
ad.rb
class Ad < ActiveRecord::Base
belongs_to :state
end
state.rb
class State < ActiveRecord::Base
has_many :ads
end
controller
#ads = Ad.all
view
<% #ads.each do |ad| %>
<tr>
<td><%= ad.title %></td>
<td>
<%= ad.state.name %>
</td>
</tr>
<% end %>
I think you need to put this: ads.state_id = states.id like this: #{ads.state_id = states.id}
The #{ } will evaluate the ruby code inside. Otherwise what you have is just text inside a string.
I'm not quite sure what your issue is, though, so I'm not totally sure that will fix it.