How to convert Form of old Rails in Rails 3.2 - ruby-on-rails

I want to implement this simple form but its in old rails. Can somebody help me out to convert it in Rails 3.2?
<%= start_form_tag ({:action => 'uploadfile'},:multipart => true) %>
<p><label for="upload_file">Select File</label> :
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<%= end_form_tag %>

<%= form_for(:upload, :url => { :action => "uploadfile" }, :multipart => true) do |form| %>
<p><%= form.label :datafile, "Select File" %> :
<%= form.file_field 'datafile' %></p>
<%= form.submit "Upload" %>
<% end %>

Related

how to Edit a post in rails and display said post in the text_field

I am trying to build a page that will host the content of the post from the psql data base and allow user to edit the content of that post inside the text field
I am getting a no method error however message is the correct column in database
any help is very appreciated
<h1>Edit message</h1>
<%= form_with(modle: #post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => form.message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_url %>
<h1>Posts shown below</h1>
<p>To add a new post click link:
<%= link_to new_post_path do %>
New post
<% end %>
</p>
<p>---------------------------------------------</p>
<% #posts.reverse_each do |post| %>
<div id = "<%=post.id %>">
<p><%= post.message %></p>
<p><%= post.created_at.strftime(" Posted at %a %I:%M%p") %></p>
<p><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
<p> <%= link_to 'Update', edit_post_path(post) %> </p>
<p>---------------------------------------------</p>
</div>
<% end %>
It should be form.object.message, make changes in this line
<%= form.text_field :message, :value => form.object.message %>
or as max said, just remove the value option all together, it will still work
<%= form.text_field :message %>
Give it a try!
<h1>Edit message</h1>
<%= form_with(model: #post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => Post.find(params[:id]).message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_path %>

Reformat a form_tag to form_for to use formaction

I have a form_tag in my edit view:
<%= form_tag stage_batch_path(#stage_batch), multipart: true, class: 'form-inline', role: 'form', method: :put do %>
<div class="form-group">
<%= label_tag 'csv_batch_file', 'Select batch file' %>
<%= file_field_tag 'csv_batch_file', class: 'form-control' %>
</div>
<br>
<div class="form-group">
<%= label_tag 'potential_item_id', 'Input item id' %>
<%= text_field_tag "potential_item_id" %>
</div>
<br>
<%= button_tag 'Stage', class: 'btn btn-primary' %>
<% end %>
Currently it puts to stage_batches/:id which is what I want.
However, I want to add another button that posts to some other controller, say Foo#create. I read in another answer that the formaction option will work. But the given example uses form_for and not form_tag:
<% form_for(something) do |f| %>
...
<%= f.submit "Create" %>
<%= f.submit "Special Action", formaction: special_action_path %>
<% end %>
How do I rewrite my form_tag as a form_for?
<%= form_tag stage_batch_path(#stage_batch), multipart: true, class: 'form-inline', role: 'form', method: :put do %>
==>
<%= form_for #stage_batch, url: stage_batch_path(#stage_batch), multipart: true, class: 'form-inline', role: 'form', method: :put do |f|%>
Also the same as:
<%= form_for #stage_batch, class: 'form-inline' do |f|%> #if #stage_batch is new_record? then method: :post, else method: :put
PS: You should try this https://github.com/plataformatec/simple_form, a lifesaver.
With simple_form your form can be transformed to:
<%= simple_form_for #stage_batch do |f|%>
<%= f.input :csv_batch_file, as: :file %>
<%= f.input :potential_item_id %>
<%= f.submit 'stage' %>
<%end%>
simple and beautiful.

Ruby on Rails - How to make one form action create two entries in differents tables

Im creating a match, that contains maps (you can see in the 5th line)
<%= form_for(#match) do |f| %>
<%= f.datetime_select :time %> </div>
<%= f.collection_select(:event_id, Event.all, :id, :name) %>
<%= f.number_field :best_of, min: 1, :style => "width: 50px;", :class => "bo-input" %>
<%= collection_select(:round, :map_id, Map.all, :id, :display_name) %>
<%= f.submit %>
<% end %>

Extend forms without page reload, rails

I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>

ruby on rails select not defaulting to current value

I'm currently using a select as follows (within a form):
<% form_for :search, :url => search_path, :html => {:method => :get} do |f| %>
<%= select('search', :type, options_for_select(['Artist', 'Track'])) %>
<%= f.text_field :query %>
<% end %>
This works, but when I perform a search, it defaults back to artist even if the user selected Track before searching. How can I correct this?
Automatically selecting the current value works for radio buttons:
<% form_for #search, :url => search_path, :html => {:method => :get} do |f| %>
<p class="radio_button">
<%= f.label :type_track, 'Search tracks' %>
<%= f.radio_button :type, 'Track' %>
</p>
<p class="radio_button">
<%= f.label :type_artist, 'Search artists' %>
<%= f.radio_button :type, 'Artist' %>
</p>
<p class="text_field">
<%= f.label :query, 'Search query' %>
<%= f.text_field :query, :class => 'auto_focus' %>
</p>
<p class="submit">
<%= submit_tag 'Go' %>
</p>
<% end %>
Any help getting this to work will be greatly appreciated!
You must use the select form builder:
<%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>

Resources