ruby: updating file upload markup - cannot work it out - ruby-on-rails

i have only been introduced to ruby this week on a new clients project and been updating some file inputs however i have got to one that is different and cannot work out how to write into the new markup i need. can anyone lend a hand?
currently it is:
<div class="input inline">
<% if host_group %>
<%= label :upload_csv_of_hosts, t(:upload_csv_of_hosts), t(:upload_csv_of_hosts) %>
<% else %>
<%= label :upload_csv_of_participants, t(:upload_csv_of_participants), t(:upload_csv_of_participants) %>
<% end %>
<div class="markup">
<%= file_field_tag "csv" %>
<%=show_required %>
<%= host_group ? show_info('group/hosts_csv') : show_info('group/participants_csv') %>
</div>
</div>
which is just using file_field_tag "csv" but i need to build that into this markup like i have for the others i converted, the others i converted was done in a different way that the one above so unsure what that one does and how to implement it into the required markup below.
<div class="input inline">
<%= f.label :filename, t(:uploaded_data), :class => "double" %>
<div class="markup">
<%= f.text_field :filename, {:size => 20, :readonly => true, :class => "file_input_textbox", :value => "No File Selected"} %>
<div class="file_input_div">
<%= f.button :browse, :class => "button button-red file_input_button" %>
<%= f.file_field :filename, {:size => 20, :class => "file_input_hidden"} %>
</div>
</div>
<%=show_required%>
<%=show_info('document/uploaded_data') %>
</div>
i have tried this....
<div class="input inline">
<% if host_group %>
<%= label :upload_csv_of_hosts, t(:upload_csv_of_hosts), t(:upload_csv_of_hosts) %>
<% else %>
<%= label :upload_csv_of_participants, t(:upload_csv_of_participants), t(:upload_csv_of_participants) %>
<% end %>
<div class="markup">
<%= f.text_field :csv, {:size => 20, :readonly => true, :class => "file_input_textbox", :value => "No File Selected"} %>
<div class="file_input_div">
<%= f.button :browse, :class => "button button-red file_input_button" %>
<%= f.file_field :csv, {:size => 20, :class => "file_input_hidden"} %>
</div>
</div>
<%=show_required %>
<%= host_group ? show_info('group/hosts_csv') : show_info('group/participants_csv') %>
</div>
but i get the following error:
undefined local variable or method `f' for #<#<Class:0x00000007672a58>:0x00000007631a30>
its 1:25am and really want to crack this, so very thankful to any helpers!
Thanks

If you are getting this error:
undefined local variable or method `f' for #<#:0x00000007631a30>
When you run this line:
<%= f.text_field :csv, {:size => 20, :readonly => true, :class => "file_input_textbox", :value => "No File Selected"} %>
And you start your form like this:
<%= form_for(:group, :url => url, :html => { :snipped => true }) do %>
Then you need this instead:
<%= form_for(:group, :url => url, :html => { :snipped => true }) do |f| %>
You can't use the form builder object if your form_for block doesn't yield it!
Also do you see how much of relevant bits of code here were not in your original version of the question? :)

Related

undefined method `permit' for "<p>tinymce</p>":String

Im trying to implement tinyme gem and I get this error:
undefined method `permit` for "<p>tiny mce</p>":String
This is the controller:
def note_params
params.require(:note).permit(:contact_id, :note)
end
The View:
<%= simple_form_for(#note) do |f| %>
<%= f.error_notification %>
<div class="form-group hidden-xs-up">
<label><i class="fa fa-address-book-o" aria-hidden="true"></i> Contact</label>
<%= f.select :contact_id, get_contact.collect{#contact.id},{}, class:"form-control" %>
</div>
<div class="form-group over-hide">
<label><i class="fa fa-file-text-o" aria-hidden="true"></i> Note</label>
<%= text_area_tag :note, "", :class => "form-control tinymce", :rows => 10, :cols => 60 %>
<%= f.button :submit, class: "btn btn-info mt-2 float-sm-right" %>
</div>
<% end %>
How should I fix this issue ?
Your form send the note directly as a root element and not nested in another note. This happens, because you use text_area_tag instead of the text_area form helper.
Just change:
<%= text_area_tag :note, "", :class => "form-control tinymce", :rows => 10, :cols => 60 %>
to
<%= f.text_area :note, :class => "form-control tinymce", :rows => 10, :cols => 60 %>

Active Merchant and f.submit button on the same view

I have active merchant the i am using simple form.
Is there a way that i can submit the form and also submit the simple_form data at the same time?
Example:
<%= simple_form_for #billing, url: checkout_shopping_cart_path do |f| %>
<h3>Billing & Registration Details</h3>
<hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="inputFirstName" class="control-label">Title:</label>
<div>
<%= f.input :title, collection: [ 'Mr', 'Mrs', 'Ms' ], selected: 'Mr', label: false %>
</div>
</div>
</div>
<%= f.submit 'Submit', class: 'btn btn-default-1' %>
<% end %>
</div>
<% payment_service_for #order.reference_id, 'XXXXXXXXXXXX',
:amount => #order.total*100.round,
:currency => 'SGD',
:credential2 => 'xxxxxxxxxx',
:service => :ipay88 do |service| %>
<% service.customer :first_name => #user.account.first_name,
:last_name => #user.account.last_name,
:email => #user.email,
:phone => #user.phone %>
<% #shopping_cart.shopping_cart_items.each do |shopping| %>
<% service.description shopping.item.name %>
<% end %>
<% service.return_url url_for(:only_path => false,
:controller => 'pay', :action => 'return') %>
<%= submit_tag "Pay", class: 'btn-default-1' %>
<% end %>
The result that i have is having two buttons. How do i make it all at one page?

Rails 4 - How to populate remote content in a form?

Here is what I have
I have a method like this in my bookmarks_controller.rb
def fetch
#result = LinkThumbnailer.generate(params[:url])
end
I have the following form fields to create new bookmarks
url
title
description
thumbnail
tags
So my new.html.erb looks like this
<%= form_for(#bookmark, :html => {class: "panel-body"}) do |f| %>
<% if #bookmark.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#bookmark.errors.count, "error") %> prohibited this bookmark from being saved:</h2>
<ul>
<% #bookmark.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="block">
<%= f.label :url, :class => 'control-label' %>
<%= f.text_field :url, :class => 'form-control', :placeholder => 'URL' %>
</div>
<div class="block">
<%= f.label :title, :class => 'control-label' %>
<%= f.text_field :title, :class => 'form-control', :placeholder => 'Title' %>
</div>
<div class="block">
<%= f.label :description, :class => 'control-label' %>
<%= f.text_area :description, rows: 6, :class => 'form-control', :placeholder => 'Description' %>
</div>
<div class="block">
<%= f.label :thumbnail, :class => 'control-label' %>
<%= f.text_field :thumbnail, :class => 'form-control', :placeholder => 'Thumbnail url' %>
</div>
<div class="block">
<%= f.label :tag_list, :class => 'control-label' %>
<%= f.text_field :tag_list, :class => 'form-control', :placeholder => 'Tags (separated by commas)' %>
</div>
<div class="actions">
<%= f.submit :class => "btn btn-info" %>
</div>
<% end %>
I would like to have a button called Fetch right next to url field.
When user click Fetch button, I would like to populate the form fields using ajax.
I viewed rails documentation. It says to add remote: true attribute to the form.
But I wanna use ajax function only when the user click fetch button.
Can someone help me with this?
Thankyou
You can generate an object using the LinkThumbnailer gem and than convert it to json
with this action:
def fetch
#result = LinkThumbnailer.generate(params['given_url'])
respond_to do |format|
format.json do
result.to_json
end
end
end
And in your view you could use the Jquery getJSON method to access the #fetch action and pass the given url as a parameter:
<script type="text/javascript">
require(['jquery'], function($) {
$('.fetch-button-class').change(function() {
var given_url = $('.url-class').val();
$.getJSON('<%= fetch_action_url %>', {'given_url': given_url }, function(obj) {
$('.title-class').val(object.title);
$('.description-class').val(object.description);
});
});
});
</script>

Form not updating values in the database if the class of a framework is used

I m using the ink framework for styling my web application. And for styling form i am using this http://ink.sapo.pt/index.php/forms.
<form class= "ink-form inline">
<%= form_for(#document, :remote => true) do |f| %>
<fieldset>
<div class="control required validation error">
<%= f.label :title, :class => "short" %>
<%= f.text_field :title, :class => "short" %>
</div>
</fieldset>
<% end %>
</form>
In the above form ive put the class= ink-form inline to style the form. Doing this styles my form but it does nt update the values in the database. When i use this code
<%= form_for(#document, :remote => true, :class => "ink-form inline") do |f| %>
then it updates the values in the database but it doesnt style the form. I am not getting where the problem is. Please help.!!
Try this!
<%= form_for(#document, :remote => true, :html => {:class => "ink-form inline"}) do |f| %>
<fieldset>
<div class="control required validation error">
<%= f.label :title, :class => "short" %>
<%= f.text_field :title, :class => "short" %>
</div>
</fieldset>
<% end %>
If you had inspected your earlier code in the browser, you would see two form tags (one within the other). The reason is form_for generates its own form and that is where you would want to provide the options.

How can I set up collection select correctly?

With the code below, if I hit Search it takes me to these url
With FireFox > http://example.com/shop?utf8=%E2%9C%93&genre[]=1
With Safari > http://example.com/shop?utf8=✓&genre%5B%5D=1
How can I remove this "[]" and "%5B%5D"?
Only I want is "&genre=1"
View code
<%= form_tag communities_path, :method => :get, :class => 'form-search' do %>
<div class="input-append">
<%= collection_select :genre, nil, Genre.all, :id, :name %>
<button type="submit" class="btn">Search</button>
</div>
<% end %>
<%= select_tag :genre, options_for_select(Genre.all.map{ |g| [g.name, g.id] }) %>

Resources