I have two Model Product and Supplier.
Product has_one supplier.
how to Build a Seach form inside a form.
,I want to Search Supplier and Put that id into My Product form.
Currently my code look like this.
<%= f.text_field :product_code, class: 'form-control'%>
<%= f.text_field :product_name, class: 'form-control'%>
<%= f.text_field :supplier_id, class: 'form-control'%>
<%= render :partial => 'supplier_search' %>
<%= f.submit 'Save', :class=>'button add'%>
Render suplier_search partial after submit button of product form.
<%= f.text_field :code, class: 'form-control'%>
<%= f.text_field :supplier_id, class: 'form-control'%>
<%= f.submit 'Save', :class=>'button add'%>
<%= render :partial => 'supplier_search' %>
Or
Don't have form in suplier_search partial. Just put input field or whatever you want, there and submit those fields via Ajax and get your required results in product form.
if its not partialing for <%= render :partial => 'supplier_search' %>
then use
"your views folder name/supplier_search" %>
Related
I'm trying to set up a page in Ruby on Rails which accepts a lot of information from the user via forms then saves it (& also allows fields to be editable). I've set up forms before but only for a single title/content form, what I want will be a page that will have a form for:
Personal Details: Name, Age, Contact Details
Company Details: Name, Age, Contact Details
I'm not sure at all how to do this, if anyone could link me to a guide that explains how to do this or can explain it to me it would be much appreciated.
It sounds like you have "Company" in a separate model. If so, you need something similar to this code. Your controller will need to initiate both #user and #company.
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: #user %>
<h3>Yourself</h3>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :age%>
<%= f.text_field :age, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<h3>Your Company</h3>
<%= fields_for(#company) do |c| %>
<%= c.label :name %>
<%= c.text_field :name, class: 'form-control' %>
<%= c.label :age%>
<%= c.text_field :age, class: 'form-control' %>
<%= c.label :email %>
<%= c.email_field :email, class: 'form-control' %>
<% end %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
For such a task you can just use scaffold
rails g scaffold User age name street zip company_name etc..
Type this into your console
This will create a complete scaffold for you. You can create and edit your form.
It's a good way to start if you have no other idea.
In my application, lists has_many books and books belong_to a list. I have this form for creating a new book in my rails application:
<div class="formtastic-control">
<%= semantic_form_for(#book) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :list, :as => :select, :include_blank => false %>
<%= f.text_field :title, class: 'form-control', placeholder: "Title" %>
<%= f.text_field :author, class: 'form-control', placeholder: "Author (optional)" %>
<%= f.submit "Add to library", class: "btn btn-default" %>
<% end %>
</div>
So the user can select which list they want to add the book to. I'm using formtastic to do that. The whole thing works fine, but I want to get rid of the word "list" which appears next to the select tag:
How can I do that? I know it's trivial, but it pushes the select tag to the right and I don't want it to do that.
I added :label => false and it worked, thanks.
I'm trying to submit a form that contains fields for both an event and an invitation to that event. Here is my form:
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :start_at %>
<%= f.input :end_at %>
<%= f.input :all_day %>
<%= f.hidden_field :owner_id, value: current_user.id %>
<% if false %>
<%= f.association :sitter, label_method: lambda { |s| "#{s.name}" }, collection: User.all %>
<%= f.association :group, label_method: lambda { |g| "#{g.group_name}" }, collection: Group.where(:owner_id => current_user.id) %>
<% end %>
<%= f.input :user_emails, as: :text %>
<%= form_tag event_invitations_path, :method => :post do %>_tag :user_emails %>
</div><div>
<%= label_tag "Your message:" %>
</div><div>
<%= text_area_tag :email_message %>
<% end %>
</div>
</br>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
This is (perhaps obviously) not working. :user_emails is NOT part of the event or invitation model as it's a list of emails that will be used to create invitations. Basically I merged two forms, one that was accepting the email/send invitation piece and one that was accepting the event information. I think I have my controller and model set up properly to take care of this but how do i submit this information as part of the same form without getting an "undefined_method" error (since user_emails doesn't belong to events). Let me know if you want to see my model/controller.
Use FormTagHelper
<%= label_tag "User", "Email"%>
<%= text_area_tag "user_emails", "example#example.com"%>
See the documentation for more options on text_area_tag and label_tag
I have a simple Rails form that allows for its associated parent to be edited. I would like to allow the user to submit this section of the form using :remote => true so that the user can add a new parent, and then select the parent in an updated select menu elsewhere in the form. As you can see in the code, I added a submit button to the parents' part of the form, and it even knows whether to say "Create" or "Update," but when I submit it the entire page is refreshed, the entire form is submitted for validation and so forth. How can I accomplish what I want in Rails?
Here is the code in question:
<%= form_for #sermon, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.text_field :date %>
</div>
<div class="field">
<%= f.label "Speaker" %><br />
<%= f.select :speaker_id, Speaker.all.collect {|p| [ p.name, p.id ] }, {:include_blank => true} %>
</div>
<% #sermon.build_speaker unless #sermon.speaker %>
<%= f.fields_for :speaker, :remote => true, :html => {:data_type => 'html', :id => 'create_speaker_form'} do |g| %>
<%= g.label :name, "Or, add a new speaker:" %><br />
<%= g.text_field :name %>
<%= g.submit %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
probably your question has been answered here How do you submit a rails 3 form without refreshing the page?
if that doesn't help you can try using preventDefault() attaching it to your submit button.
I'm making a simplistic message board with tags. The message#index view displays a list of all messages. The tag#show view shows messages of a specified tag. On the message#index view, there is a form (partial) that requires the user to write a message and to tag it. On the tag#show view, I'd like to use the same form partial but to have the view's tag automatically filled into the form. In the show action of the tags controller, the name of the tag is #title.
The form partial looks like this:
<% form_for :message, :url => { :action => "create" }, :html => { :id => 'form' } do |f| %>
<%= f.error_messages %>
<%= f.label :tag, "tag<p2>( separate tags with a comma )</p2>" %>
<%= f.text_field :tag_list %>
<%= f.label :name, "name<p2>( optional )</p2>" %>
<%= f.text_field :name %>
<%= f.label :email, "email<p2>( optional )</p2>" %>
<%= f.text_field :email %>
<%= f.label :title, "message" %>
<%= f.text_area :content %>
<%= f.submit 'submit' %>
<% end %>
How do I auto fill the tag_list text field with the #title value? #title is a string. I appreciate any help you can offer. Thank you.
<%= f.text_field :tag_list, :value => #title %>