My View:
<div class="field">
<%= label :isbn_exist, 'ISBN' %>
<%= text_field :isbn_exist,'ISBN' %>
</div>
<%= link_to 'Add new book', new_book_path(:isbn_exist => #isbn_exist), :class => "btn btn-warning"%>
This ":isbn_exist" variable is not sent to the controller. why? If i put a number instead of #isbn_exist this value is sent. I think the text_field is not saving the value on the :isbn_exist variable...
You have a text_field and a link. What you really want is a form (including this text_field) and a submit button.
Things like this are covered in every rails tutorial you can find.
Related
I'll try to ask this as clearly as I can. I'm doing a Udemy course and we are using nested fields to take in data. The form renders a single nested field initially and then there is a link to add additional fields. This all works and there are no errors in the console. If i put something in the first nested field, and add two more nested fields, only what was entered in the last two nested fields gets saved. When I look at the final result after saving, the first item is not there. I'm sure this has to do with how I'm rendering the initial field, but I can't figure out what it is. Both the initial render and the link_to_add_nested are using the same partial file.
Screenshot of the form
Code from _form.html.erb:
<div>
<%= form_with model: #technologies do |form| %>
<%= render partial: 'technology_fields', :locals => { :form => form } %>
<% end %>
</div>
<div>
<%= link_to_add_nested(form, :technologies, '#Add-Technology', partial: 'technology_fields') %>
</div>
<div class="form-group">
<%= form.submit "Save Portfolio Item", class: 'btn btn-primary btn-block'%>
</div>
<% end %>
</div>
Code from the partial _technology_fields.html.erb:
<div class="form-group nested-fields" id=Add-Technology>
<%= form.label :name %>
<%= form.text_field :name, class: 'form-control' %>
<%= link_to_remove_nested(form, link_text: 'Remove Item') %>
</div>
I can add code from the models and controller if needed, but as the form is working with the dynamically added nested fields, I don't think that's where the problem lies.
Bonus question: The same form is used for new and edit actions. Ideally when the form is used to edit an existing item, I would like the page to render with fields populated with the existing data. As it currently stands I have no way to delete or modify once it has been saved. I can only add new entries.
Thank you very much for any help you can give. I've done a lot of searching but haven't been able to locate the right information.
I want to pass one variable which is introduced by the user in the front-end. I dont use any model (i dont need it, because im working with JSON data all in memory). I have looked many tutoriasl but almost all of them are focused on filling out a form. My application does not have any form nor tables. Any idea?
Thank you.
{<div class="module1">
<p>Mein Lastprofil berechnen</p>
<div class="boxed">
Jahreshausverbrauch (kWh)
<%= text_field_tag "input", nil, placeholder: "3500" %>
<%= button_to "Senden", root_path, :method => :get %>
</div>
</div>}
I want to save/pass the variable introduced when clickling the button.
By having a text field you implicitly already have a form.
Make it explicit by wrapping the text field and button:
<%= form_tag root_path, method: :get do %>
<%= text_field_tag :input, placeholder: "3500" %>
<%= submit_tag "Senden" %>
<% end %>
Then you can access the value as params[:input] in the controller.
Sorry for this rather simple question, but I know almost nothing of rails.
What I want to do is that when I'm registering an "Articulo" I would like to choose some default values that they would be given by an attribute of a model, specifically, the attribute "rut_prov" from the model "Proovedor".
This is what I have.
articulos_controller.rb
def new
#articulo = Articulo.new
#proveedors = Proveedor.all
respond_with(#articulo)
end
articulos/new.html.erb
<div class="container">
<h1>Nuevo artículo de venta</h1>
<%= render 'form' %>
articulos/_form.html.erb
<div class="field">
<%= f.label :rut_del_proveedor %><br>
<%= f.select :rut_prov, #proveedors, :class => 'genForm_dropBox' %>
</div>
view
So, I know the problem is <%= f.select :rut_prov, #proveedors, :class => 'genForm_dropBox' %>, but I don't know how to make appear as options the values of "rut_prov" of the records I have registered of the model "Proovedor".
You can use collection_select method. The following example displays the result of rut_prov method call on view and passes rut_prov of selected as param:
<%= f.collection_select :rut_prov, #proveedors, :rut_prov, :rut_prov, class: 'getForm_dropBox' %>
BTW, I strongly recommend using English variables/classes/methods names. So it should be Provider instead of Provedoor, Article instead of Articulo etc.
I have been recently working with Ruby on Rails and have run into an issue that I can not quite figure out. I need to create a bunch of form mockups, that do not function. That is they should have the submit button, but it should not do anything upon being clicked. Normally using html I would do something along the lines of
<form action="#">
</form>
Trying to convert this to use Rails form helpers, I have done the following
<%= form_tag "#" do %>
<%= label_tag :username, "Username: " %>
<%= text_field_tag :username %>
<br />
<%= label_tag :password, "Password: " %>
<%= password_field_tag :password %>
<br />
<%= submit_tag "Login" %>
<% end %>
This generates a form that is similar to what I want to achieve, however when clicking the submit button it tries to access /# via post which is not the desired result. Currently the only thing I can think of to achieve this is to set the disabled attribute of the button, but is there a better way?
Unfortunately this can't be achieved with form helpers. Defining a form_for or a form_tag requires an action for the form. You can set
:action => "#"
But this will require including the action in routes -> having a controller with action for it -> rendering some page yet again.
You could manipulate the form after loading with javascript however (sust remember to set :remote to true - ). Or alternatively, if you insist on using the form helpers - replace the submit_tag with a button_tag:
<%= button_tag "Login", :type => 'button'%>
Try
<% form_tag "#", :onSubmit => "return false" do %>
Have you tried with button_tag instead of submit_tag? See here. Just make sure you don't use the default, or you will be right back where you started.
I am looking for the best approach to this problem. I have two search forms on the same page ( they each search a a different api for info) and I would like to have one submit button and then the relevant api is called dependent on which form has content. So I though I could specify the controller action when submitting on each form like so
<div class="container margin50">
<div class="row">
<div class="span6 offset3 cf formBackground">
<h1>CoverArt Finder</h1>
<h3>Search Movies</h3>
<%= form_tag main_results_path, :method => "get" %>
<%= text_field_tag 'search', nil, :placeholder => 'Enter Film Name Here.....' %>
<h1>OR<h1>
<h3>Search Albums</h3>
<%= form_tag album_album_results_path, :method => "get" %>
<%= text_field_tag 'search', nil, :placeholder => 'Enter Artist Name here.....' %>
<%= submit_tag "search" %>
</div>
</div>
</div>
Obviously this is not working as i always get the results for the movie search parameters. Do i need a conditional statement in there to recognise which form is filled in? I’m a little unsure here.
Any other info needed please ask
Any help appreciated
Thanks
If you're willing to do this client side (JavaScript / jQuery) it shouldn't be much hazzle. On clicking the submit button you could check which form has an value. Some simple sudo code:
on submitButton click:
if formA.someValue != null
post / submit formA
else if formB.someValue != null
post / submit formB