I have a text field and i want to process the entry after clicking on submit.
The text field is:
<%= text_field :reason_message, params[:reason_message] , placeholder: "Write a reason" %>
The form is:
<%= form_tag main_admin_controller_path, :method => 'post' do %>
And the submit tag is:
<%= submit_tag "Send", name: 'send', data: { disable_with: "Loading..." },class: 'btn btn-primary btn-xs' %>
To get the entry I take the parameters as:
params["reason_message"]
which is returning an array ["test"] or if nothing is written [""].
How can I get the params directly as a string and not as an array?
Right now I do params["reason_message"].join to get it as a string. Is there another way?
Related
In my view, using Slim:
= form_with url: update_pn_price_path(#price)
.form-group
= label_tag :part_number
= text_field_tag :part_number, #price.part_number, required: true
.form-group
=> submit_tag t(:save), class: 'btn btn-primary btn-sm'
= link_to 'Cancel', '', class: 'btn btn-link btn-sm', data: { cancel_in_place: true }
This always submits using a standard submit, not remotely. Log shows:
Processing by PricesController#update_pn as HTML
What am I doing wrong? Why won't it submit using JS?
I'm surprised to see that your view compiles. There are a few problems that I can spot.
You start indenting content as though it would be in the form. However you didn't open a form tag or provided a block to the form_with method.
= form_with url: update_pn_price_path(#price)
.form-group
Should at least be changed to:
= form_with url: update_pn_price_path(#price) do
.form-group
Since a variable is provided to a block, the preferred way is to capture it in a variable (form in the code below). If you're not planning to use it you should still capture it in an underscored variable to indicate that it is there, but not being used (_form).
= form_with url: update_pn_price_path(#price) do |form|
.form-group
Since you're using a form builder, the preferred way of creating elements is through the form builder interface. Since you're not creating the submit tag through the form builder this might be another reason why the submit isn't done remotely.
I would at least replace:
=> submit_tag t(:save), class: 'btn btn-primary btn-sm'
With:
= form.submit t('save'), class: 'btn btn-primary btn-sm'
But preferably update the whole form to use the form builder. The result should look something like this:
= form_with model: #price, url: update_pn_price_path(#price) do |form|
.form-group
= form.label :part_number
= form.text_field :part_number, required: true
.form-group
= form.submit t('save'), class: 'btn btn-primary btn-sm'
= link_to 'Cancel', '', class: 'btn btn-link btn-sm', data: {cancel_in_place: true}
For more/other form builder methods see the FormBuilder and FormHelper documentation.
Here's what solved the issue. Rookie mistake, but posting here in case someone else runs into it.
My form shown above was inside another form. You can submit such an 'inner' form, but not remotely. The form was being inserted via an AJAX call, so I changed that to insert to the end of the HTML body, and then positioned using CSS. That put it outside the containing form and made things work as expected.
I have a button_tag that contains a call to action and a phone number for something that should read "Call Us at 1-800-000-0000." I have the phone number stored as a variable in the application helper. I'm having trouble merging it in. This is what I've tried
View
Tried
<%= button_tag 'CALL #{phone_number}', {class: 'btn btn-u margin-bottom-20 form-center-button'} %>
Also Tried
<%= button_tag 'CALL <%= phone_number %>', {class: 'btn btn-u margin-bottom-20 form-center-button'} %>
Application Helper
def phone_number
'1-800-000-000'
end
This should work:
<%= button_tag "CALL #{phone_number}", {class: 'btn btn-u margin-bottom-20 form-center-button'} %>
Note the double quotes. If you want to interpolate variables in a string, you must use double quotes.
String concatenation would also work:
<%= button_tag "CALL " + phone_number, {class: 'btn btn-u margin-bottom-20 form-center-button'} %>
In my rails 4 erb view i have this link,
<%= link_to "Download #served_file.title", "/public/uploads/#served_file.file", :class => "btn" %>
However, it won't replace #served_object.title and #served_object.file with the text but just placing those lines inside a <%= %> works as expected.
You need to interpolate the values into the string.
<%= link_to "Download #{#served_file.title}", "/uploads/#{#served_file.file}", :class => "btn" %>
I have a Rails app that includes Requests and Workorders tables.
I have a New Workorder button on the Request show page. I need to pass information from the Request to the Workorder - for instance the Request.id.
I'm currently using flash to do this. Here is the button on the Request show page:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => (flash[:request_id] = #request.id %>
In the new Workorder form, I have:
<% if flash[:request_id] != nil %>
<%= f.hidden_field :request_id, :value => flash[:request_id] %>
This works. But, not always. And I haven't been able to figure out why it fails sometimes.
Is there a better way to pass this data?
Thanks for the help!!
UDPDATE1
Sometimes I need to bring forward quite a few data fields. For example:
<%= link_to 'Follow-up Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => ( flash[:workorder_id] = #workorder.id, flash[:client_id] = #workorder.client_id, flash[:contact_id] = #workorder.contact_id, flash[:location_id] = #workorder.location_id, flash[:type_id] = #workorder.type_id, flash[:woasset_id] = #workorder.woasset_id) %>
You can try passing the parameter to the path of the link and then pass it to the form via your controller's action:
Link:
<%= link_to 'New Work Order', new_workorder_path(request_id: #request.id), :class => 'btn btn-primary' %>
Controller
def new
#request_id = params[:request_id]
...
end
In your view:
<%= f.hidden_field :request_id, value: #request_id %>
I need use button_tag instead submit_tag on a form to add icon with style http://twitter.github.com/bootstrap/base-css.html#buttons.
<%= simple_form_for(bla..........bla......)) do %>
<%= button_tag t('.sent_to_trash'), :class => "btn btn-small btn-primary disabled", :id => "trash_button", do %>
<i class="icon-trash icon-white"></i>
<%= t('.sent_to_trash') %>
<% end %>
<% end %>
The question is I can not receive the params[:commit] with button_tag, however with submit_tag is working fine and I receive correctly params[:commit] on my action controller.
How can I fix this problem?
Thank you very much!
Html element button works with :name and :value params, so you have to explicitly define these, e.g.
= button_tag(:name => "commit", :value => "my_button") do
= "Press me!"
then you get params[:commit] = "my_button" after form submit.
Note: You should specify :type attribute too, because different browsers use different default types for the <button> element (:type => "submit")