Active Merchant and f.submit button on the same view - ruby-on-rails

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?

Related

Rails 4 - Simple form arguments

Can anyone see what's wrong with this form of expression?
<%= simple_fields_for :article do |f| %>
<%=f.input :q, :nil, placeholder: "Search" %>
<% end %>
Error message says:
wrong number of arguments (3 for 1..2)
So- my wider code is:
articles#index.html.erb:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<%= render 'articles/searchform' %>
</div>
<div class="col-md-10 col-md-offset-1">
<% Article.all.sort_by(&:created_at).in_groups_of(3) do |group| %>
</div>
<div class="row">
<% group.compact.each do |article| %>
<div class="col-md-4">
<div class="indexdisplay">
<%= image_tag article.image_url, width: '100%', height:
'200px' if article.image.present? %>
<div class="indexheading"> <%= link_to article.title, article %> </div>
<div class="indexsubtext"> <%= truncate(article.body, :ommission =>
"...", :length => 250) %></div>
</div>
</div>
<% end %>
</div>
<div class="row">
<%= link_to 'New Article', new_article_path %>
</div>
In my views articles#form.html.erb
<div class="col-xs-10 col-xs-offset-1">
<%= simple_form_for(#article) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, autofocus: true %>
<%= f.input :body, :label => "Post", :input_html => {:rows => 10} %>
<%= f.input :image, :label => "Add an image" %>
</div>
<div class="form-actions">
<%= f.button :submit, "Review a draft", :class =>
'formsubmit' %>
</div>
<% end %>
In articles#searchform.html.erb
<% f.simple_fields_for :article do |a| %>
<%=a.input :q, :nil, placeholder: "Search" %>
<% end %>
I get this error:
undefined local variable or method `f' for #<#<Class:0x007f874133c410>:0x007f8740448058>
You are missing your form variable, e.g. f.
For instance if you have simple_form_for ... do |f| you should then write
= f.simple_fields_for :article do |article|
= article.input :q, :nil, placeholder: 'search'
[EDIT] You seem confused. The simple_fields_for is a special command to iterate over nested items, inside a form (e.g. comments for a post). So simple_fields_for would require a surrounding form (simple_form_for). For your search-form you should just be using simple_form_for.

Prepopulate complex form values in rails edit view

I have an assessment form that renders a list of questions, and accepts answers for each question. The form data is submitted manually via ajax.
I'm having an issue when trying to pre-populate the data in my edit view. Here's how my views look:
edit.html.erb:
<div class="container">
<%= render 'assessment_form', :locals=> {:assessment => #assessment} %>
</div>
_assessment_form.html.erb:
<%= form_for #assessment do |f| %>
<%= f.submit 'Save', :class=> "btn btn-primary", :style=>"width: 100%;" %>
<div class="col-sm-2 col-sm-offset-10">
<h4>Show on Followup?</h4>
</div>
<% #assessment.questions.where(category:"S").each do |question| %>
<%= render 'question_fields', :question=> question, :f=> f %>
<% end %>
<%= f.hidden_field :patient_id, :value=> #assessment.patient.id %>
<%= f.hidden_field :template_id, :value=> #assessment.template_id %>
<% end %>
_question_fields.html.erb:
<p><%= question.content %></p>
<%= f.fields_for :answer do |builder| %>
<div class="row question">
<div class="col-sm-2 pull-right toggle">
<%= builder.check_box :tracking, {:checked=> false, :class=>"trackable", :data => {:'on-text' => "Yes", :'off-text' => "No", :'on-color'=> "success", :question => question.id}}, 0 , 1 %>
</div>
<div class="col-sm-10">
<%= builder.text_area :content, :class=>"form-control question-field", :data => {:question => question.id} %>
</div>
</div>
<% end %>
But none of the answers content are rendering in the view, the form fields are blank.
Here's an example call: Assessment.last.answers.first.content will give me the string "test". That should be showing up in my text_area box but it's not.
Just do, the instance variable #assessment is passed to the partial.
<%= render 'assessment_form' %>

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>

ruby: updating file upload markup - cannot work it out

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? :)

Rails how to update image? And how to do RESTful routing?

I am trying to update my photographer model. But how problems with Rails routing and paperclip wont replace image.
When I submit my form the url is: http://localhost:3000/admin/photographers/save.75 which gives an error. Couldn't find Photographer without an ID
And my image is not updated.
My form:
<%= simple_form_for #photographer, :url => save_admin_photographers_path(#photographer), :html => {:multipart => true, :method => :post} do |f| %>
<%= javascript_include_tag "tiny_mce/tiny_mce" %>
<%= javascript_include_tag "init_my_tiny_mce" %>
<%= f.input :name, :label => 'Name' %>
<%= f.text_area :text, :label => 'Text', :size => '12x12' %>
<%= f.file_field :image, :label => 'Image' %>
<% if #photographer %>
<% if #photographer.image %>
<p class="ok">
<label for="dd">image ok</label>
<%= image_tag("http://s3-eu-west-1.amazonaws.com/kjacobsen/photographer/image/#{#photographer.id}/#{#photographer["image"]}", :size => "39x22") %>
</p>
<p>
<label for="sdd"> </label>
<%= link_to("remove", {:action => "remove_image", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No image uploaded for movie
</p>
<% end %>
<br />
<% end %>
<br />
<%= f.file_field :flv, :label => 'Upload FLV' %>
<br />
<% if #photographer %>
<% if #photographer.flv %>
<p class="ok">
<label for="dd">flv: <%= #photographer["flv"] %></label>
<%= link_to("remove", {:action => "remove_flv", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
No flv uploaded
<br /><br />
</p>
<% end %>
<% end %>
<br />
<%= f.file_field :quicktime, :label => 'Upload Quicktime' %>
<br />
<% if #photographer %>
<% if #photographer.quicktime %>
<p class="ok">
<label for="dd">quicktime: <%= #photographer["quicktime"] %></label>
<%= link_to("remove", {:action => "remove_quicktime", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No quicktime uploaded
<br />
</p>
<% end %>
<% end %>
<%= f.button :submit, :value => 'Create movie' %>
<% end %>
My update controller:
def save
#photographer = Photographer.find(params[:id])
#photographer.update_attributes(params[:photographer])
if !#photographer.save
flash[:notice] = " "
render_action 'edit'
else
flash[:notice] = "update ok"
redirect_to :action => 'edit', :id => #photographer.id
end
end
My routes:
namespace :admin do
resources :photographers do
collection do
post :save
end
end
end
What you're doing is basically an update action. The update route is automatically created when you do resources :photographers, you can verify this by typing rake routes in the terminal.
You should rename your controller action from save to update and remove the custom route:
namespace :admin do
resources :photographers
end
Then use the update route in your form:
:url => admin_photographer_path(#photographer)
You should also change your html method, the update action uses PUT:
:method => :put
Hope this helps.

Resources