I'm trying get a value of phone from input and pass it by url for so call a method to delete it. I have:
<div class="telefones">
<%= f.fields_for :phones do |p| %>
<div class="control-group">
<div class="controls">
<%= p.text_field :number, :name => 'phone', :class => 'text_field', :type => "text", :data => { :mask => "99 9999.9999"} %>
<%= link_to 'Delete Phone', "/admin/clinics/"+PHONE_NUMBER+"/delete-phone" %>
</div>
</div>
<% end %>
In PHONE_NUMBER, will be where I will spend the number of this input. Any idei how to fix it?
I don't think it's possible the way you're trying to do that. I would suggest to 2 another approachs:
Send that form to an particular action, and there, you get the value of phone number input, and delete it
You could use jquery to get the input value and send a http request to that "/admin/clinics/"+PHONE_NUMBER+"/delete-phone" URL, so you could replace the PHONE_NUMBER with the value you just got.
Anyway, since that value is being inputed by the user, you should do some checks and validations. It's not quite simple.
Hope this helps you. Good luck!
Related
i have a form where users input some information and I want to add to this form a country and I am wandering what is the best way to do this (seed data, Csv, ...) i don't want to use a gem
Since this is essentially static data, I wouldn't even store it into the database. I would create a PORO that had a class method that returns a 2 dimensional array (country with country abbreviation). It's just a matter of sourcing that data.
Here's a list you can use.
https://github.com/karmi/localized_country_select/blob/master/locale/en.rb
I use the country_select gem. It is very simple to use. Just supply model and attribute as parameters:
country_select("user", "country")
Here is an example from one of my forms:
<%= f.fields_for (:address) do |b2| %>
<%= b2.input :street %>
<%= b2.input :city, :label => "City/Suburb" %>
<%= b2.input :state %>
<%= b2.input :post_code %>
<div class="control-group">
<label class="control-label">Country</label>
<div class="controls">
<%= b2.country_select :country, ["Australia", "New Zealand"] %>
</div>
</div>
<% end %>
I'm new to rails. I'm trying to create a rails marketplace where sellers can cash out their acquired funds from sales on the site.
I'm confused how to configure my withdrawal form and my orders controller.
When I simply just go to localhost:3000/withdrawal, a Stripe Recipient with just name is created on my Stripe dashboard without even completing the form. My form is nonexistent because everything I've tried for form_for generates an error.
I want the user to input their info and then choose to submit it, not create the recipient when "cash out" (which leads to the withdrawal path) is clicked.
The stripe documentation is helpful, but I'm not sure how to create my form.
Here is my withdrawal action in my orders controller. I'm wondering if I need a new action within withdrawal? But not sure if that's possible?
def withdrawal
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
recipient = Stripe::Recipient.create(
:name => current_user.full_name,
:type => "individual",
:bank_account => token
)
transfer = Stripe::Transfer.create(
:amount => (#funds).floor,
:currency => "usd",
:recipient => #seller.recipient
)
end
And my withdrawal.html.erb. I know I am missing a form tag and submit, but everything I've tried just processes an error. I don't know what form_for to call. I've tried "order" but it results in an error.
<div class="text-center">
<h1>Bank Account Information</h1>
<div class="form-group">
<%= label_tag :name %>
<%= text_field_tag :name, nil, { :name => nil, :'data-stripe' => "name", class: "form-control" } %>
</div>
<div class="form-group">
<%= label_tag :withdrawal_amount %>
<%= text_field_tag :withdrawal_amount, nil, { :name => nil, :'data-stripe' => "amount", class: "form-control" } %>
</div>
<div class="form-group">
<%= label_tag :routing_number %>
<%= text_field_tag :routing_number, nil, { :name => nil, :'data-stripe' => "routingNumber", class: "form-control" } %>
</div>
<div class="form-group">
<%= label_tag :account_number %>
<%= text_field_tag :account_number, nil, { :name => nil, :'data-stripe' => "accountNumber", class: "form-control" } %>
</div>
I'd appreciate any guidance on how to create this "cash out" action. Thanks.
form_for is for model objects. If you're not using a model object, or don't want/need Rails to infer things from the model object, don't use form_for.
For simple forms, instead use one of:
form_tag - Same syntax as form_for but without the model-related magic.
A plain old <FORM> tag. ERB templates are, after all, just HTML with Ruby mixed in.
Your view should look something like the following:
<%= form_tag withdrawl_path, method: :post do %>
<input type="text" name="card[number]" />
<!-- whatever fields you need for form -->
<% end %>
You will be able to access the submitted params via params[:card][:param_name] or use strong params and only permit params you need.
Use custom routes
get "/form" => "controller_name#action_for_form", as: :action_for_form
post "/withdrawl" => "controller_name#withdrawl", as: :withdrawl
Controller:
def action_for_form
# whatever code you need to setup form
#seller = Seller.find(params[:seller_id])
end
def withdrawl
# withdrawl code here
end
private
def card_params
params[:card].permit(:card_token, :other_params)
end
Its worth taking the time to understand the form tag and using Rails outside of the Railsy way of form submissions. It can get messy, but it allows for much more flexibility.
I am working with David Francisco's rails feedback plugin. The plugin works fine, except for one thing - I would need the feedback form to submit to the database the url for the page where the feedback form was used. Does anyone know how to do this?
The view that would need to send the current url, in addition to the currently sent information:
<h4>Feedback</h4>
<p>Please leave us feedback, it's really appreciated.</p>
<%= form_for #feedback, :as => :feedback, :url => feedback_index_path, :html => { :id => "feedback_form" } do |f| -%>
<%= f.hidden_field 'user_id', :value => current_user.id %>
<% unless #error_message.blank? %>
<p class="error">
<%=h #error_message %>
</p>
<% end %>
<p>
<%= f.label 'subject' %>
<%= f.select 'subject', ['Problem', 'Suggestion', 'Question', 'Other'] %>
</p>
<p>
<%= f.label 'comment' %><br />
<%= f.text_area 'comment', :rows => 10, :cols => 30 %>
</p>
<p><%= f.submit 'Send' %></p>
<% end -%>
Currently, feedback_index_path is always '/feedback', the url for the form.
Thank you,
Alexandra
You can use request.referer in your controller to get the path of the page that called your action. request.referer returns a full url but you can parse it with the URI module:
URI(request.referer).path
I see some people have suggested request.fullpath but this is actually the path of the action that's processing the request (in your case /feedbacks/new) and not the path where the form was submitted from.
If request.fullpath doesn't work incorrectly, you can make a quick hack, storing page url in data-attributes of page, and on submit get current url by jQuery from that attributes.
But it is a hack, just to make it working.
BTW how do you render feedback form?
feedback_index_path is a routes helper method that will always return the same thing. In your case /feedback.
Look here for info on accessing the current URL in both Rails 2 and 3.
this form_for used to work before I ported my application to rails 3.1
<div class="form-box" style="padding-left:1em;">
<%
action = #existing_mass.nil? ? "add_to_power_plant": "update_power_plant_substrate";
submit_button_label = #existing_mass.nil? ? 'Add': 'Update';
%>
<%= form_for :substrate_mass, #substrate_mass, :remote => true, :url => { :action => action, :substrate_id => #substrate_mass.substrate } do |f| %>
<div>
<%= f.label :quantity_per_year, "Quantity" %>
<%= f.text_field :quantity_per_year, :size => 5, :onclick => 'this.select();', :value => #substrate_mass.quantity_per_year %>
</div>
<div class="actions" style="float:right;">
<%= f.submit submit_button_label %>
</div>
<br/>
<% end %>
</div>
I have spent over 4 hours trying to figure out what's wrong ... there is definitely something I am not understanding anymore
I get the error:
wrong number of arguments (3 for 2)
Note that I am trying to update an variable that is not an activerecord object. It's just an object that is not stored in the database.
Hope someone can help.
cheers
form_for only takes two arguments, the record, and options, although record may be several things, including a simple symbol, an object, or an array.
Try just dropping the first symbol and sending your object. If you model does not include ActiveModel::Naming, you may set the name via the :as option.
<%= form_for #substrate_mass, :as => 'substrate_mass', ... %>
More help may be found here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Or to view the source directly:
https://github.com/rails/rails/blob/v3.1.0/actionpack/lib/action_view/helpers/form_helper.rb#L353
I got a list page and I filter items via links with get params (I can choose many links so query would be like "?param1=value1¶m2=value2"). But also I have to filter it by text field, so I made a form:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
But when I submit it, text field param replaces existing query params. So, how to make text field value add to query, not to replace it?
Since I was just dealing with this problem in Rails 4 I thought I'd share my solution.
My page gets loaded with a sport_id parameter, and when the user specifies a sort-order I wanted it to submit a GET request for page.url/event?sport_id=1&sortby=viewers but it wouldn't preserve the sport_id parameter until I added a hidden field tag in the form like so:
<%= hidden_field_tag :sport_id, params[:sport_id] %>
This solution does submit an empty sport_id parameter if that parameter was not in the original request, but that is easily prevented by encapsulating the hidden field in an <% if params[:sport_id].present? %> condition.
Use hidden_field_tag.
Inside of your form, just set hidden_field_tags for the existing GET params, like so:
<% request.query_parameters.collect do |key, value| %>
<%= hidden_field_tag key, value %>
<% end %>
This will ensure that your existing params persist.
Rails 3?
<%= form_tag your_path(params.except(:controller, :action)), :method => :get do %>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
<% end %>