Rails: Form in static pages of high voltage gem - forms

I have created few static pages with awesome high_voltage gem.
My pages are static, except a different form in each page.
Forms are supposed to grab user details and send emails (and may be save details in db)
Question:
How should I proceed with this ?
I am concerned more about rails approach,
e.g.
How can I add validation without custom coding (both at client / server side)
How can take help from rails helper method ?
Suppose, I would like to save those fields in db, how should I deal with that ?
In nutshell, I want to use as maximum of rails magic without manually dealing with things.
What I have tried ?
Currently, I have forms in each page. Each form posts at same controller (PagesController) with post method.
I then differentiate them, based on hidden input on respective form.
I do not have any validations for now.

I finally created a model without activerecord, following this great article.
I created a model like below.
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end

I have answered this post in more detail here:
Render partial from static page
Here is the commit to a demo repository to make this work:
https://github.com/harlow/high_voltage_demo/commit/0cdbca5b0fe898d27df22787498fc7e350e81422

Related

How do I generate a form using a model with single table inheritance?

I have a model Company that has a type field. There are two subclasses Fleet and Dealer.
I know the inheritance is working so I'm now working towards the views working too. However the views/companies/_form.html.erb is producing this error when I click edit on a specific company object on my webpage:
undefined method `dealer_path' for #<ActionView::Base:0x000000000418c0>
Here is the form_with tag:
<%= form_with(model: company, class: "contents") do |form| %>
*CONTENT*
<%end%>
I want to generate the form based on what company type I am looking at and for all the fields to be populated with their corresponding data.
I found this warning on the ROR site (https://guides.rubyonrails.org/form_helpers.html):
When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify :url, and :scope (the model name) explicitly.
But I do not understand exactly what this means and I believe it is the key to my solution. I wish there was an example of this situation on that guide.
Any help or suggestions are welcome and appreciated!
UPDATE
I changed the form_with tag to be:
<%= form_with(model: [:company #company], class: "contents") do |form| %>
and from my understanding this now generates the URL companies/:id/edit
This is closer, and now brings me to the companies edit page but I can't update now because the changes don't persist.
My NEW question now is, what routes should I have set up?
If I'm on localhost:3000/companies and I click 'show company', should this take me to /companies/id: or take me to /dealer/id: / /fleet/id: ?
I have managed to route the app so it will take me to /companies/id regardless of the type value of the object. This works well and all CRUD is working well now too.
I didn't log exactly what it was I changed this time as I just reverted to a previous commit and rebuilt from there.

redux-form Wizard form with linked fields

I am building a multi-step application form with React. Having first built it with pure internal state I am now in the process of refactoring to Redux using redux-form.
Having used the example here as a basis: http://redux-form.com/5.2.5/#/examples/wizard?_k=oftw7a we have come a good way.
However the problem appears when i have two forms which are supposed to have the same value. During one of the pages i have a name field, that is supposed to be duplicated on the name field of the next page. The opposite should happen if you go back from the last page. Any tips to how this could be achieved?
Using the wizard, you are basically working with the exact same form that's split into multiple pieces. Ultimately it's the same form, because redux-form tracks them by name. It is how the library identifies the pieces of the same form - using the name.
form: 'wizard',
Here you can see that the exact same instance of the form will be shared throughout the pieces. fields work in a similar manner. Each field is defined as part of a form.
As long as you use the same field constants inside the fields object that you pass into the reduxForm function and as long as the value for form is the same, so that they use the same underlying form object, it should work for you just fine.
On one page you should pass in
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'someOtherFieldThatShouldOnlyBeHere',
},
...
And then on the other page:
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'thirdFieldHere',
},
...
Also, make sure you keep destroyOnUnmount equal to false if you want to navigate back-and-forth.
Hope that helps.

RefineryCMS, Upload Image on FrontEnd side

Sorry about quite silly question but after several days of searching I still don't have the answer and I'm realy stackoverfloved about it.
I have refinerycms extension. Like this:
$ rails generate refinery:engine shop name:string logo:image
I want it to be controlled from both backend and frontend of my application.
Have no problems with backend. (Thank you for refinerycms team.)
Have no problems with frontend when the form contains only "name". (Or any other attribute that doesn't have image type).
Have problems with image type attributes.
I don't want frontend user to have the same image editing form as backend users does.
I want the form to be smth like this:
<div class='field'>
<%= f.label :logo , "Logo"%>
<%= f.file_field :logo %>
</div>
And I want images loaded by frontend user to be saved with Refinery::Image.
I bet the problem is only in my frontend controller create action for shops, but I have no clue how it should be overwritten.
Thanks a lot for any suggestion.
In the controller, you will have to pass the 'logo' param to the model as a Refinery::Image instance. To do so, you create an instance of Refinery::Image and merge it to the model.
Example:
logo = Refinery::Image.create(image: params[:shop][:logo])
#shop = Refinery::Shops::Shop.create(params[:shop].merge({logo: logo}))
I have FE form with file_field (:logo) on FE and in my normal controller (not the admin one) I have this method to handle params from request:
def model_params
permitted = params.require(:model).permit!
permitted[:logo] = Refinery::Image.create(image: permitted[:logo])
return permitted
end
to permits params and create image form file.
And then in my create method:
def create
if (#new_beer = ::Refinery::Models::Model.create(model_params))
#do some stuff when it succeeds to create it
redirect_to SOMEWHERE
else
redirect_to SOMEWHERE, notice: "Something went wrong!"
end
end
Hope it can help! :)

rails 3.1 auto-completion and realtime update in rails

Can anyone tell me how to easily implement auto-complete in rails 3.1?thanks. p.s. What's the best solution for simple_form to do this??
Besides, I also want to know how to implement meetup's RSVP instant push in rails?
I have the following form, but it didn't work, please advise.
%table
= simple_form_for(#book) do |f|
%th book name
%th
= f.input :name, label: false, placeholder: "add a name", :wrapper_html => { :id => 'autocomplete' }
= f.input :object, autofocus: true, label: false
%th
= f.button :submit, "Yes!"
I built a simple Rails 3.1 app using the jQueryUI autocompletion feature here. You simply add the jQueryUI library to your assets, add a css class or id to the actual text field that shall be autocompleted and put some javascript logic in your application, like:
$(function() {
$('#autocomplete').autocomplete({
source: '/movies/autocomplete'
});
});
with #autocomplete the css id of the text field. '/movies/autocomplete' is the actual route that will HTTp requested with the data that's put into the text field. In the movies#autocomplete action you then use that param to search for data for the autocompletion. Depending in what kind of jQueryUI autocompletion you use, you simply return an array like [{:label => 'FOO', :value => 'foo'}, ...] as JSON. label is the text you get autocompleted, value is the text that is put into the text field when clicking the label. I think and hope the code from the repo is quite understandable. Also, have a look at the JQueryUI library.
Though there shouldn't be a problem with simple_form, since it's just the text field that gets touched.
Best regards
Tobias

Rails 3.1 - Filling in Forms with Values

I have two models:
# User.rb:
has_many :currics, :dependent => :destroy
# Curric.rb
belongs_to :user
A User has the following fields associated with it:
Full Name
E-mail
City
Postcode
In the form for creating a "Curric" I want to be able to find out if this information is already set, and if it is, don't display the section of the form.
However, if it isn't set (only Full Name and Email are required at signup), I want to render a section of the form to allow the user to save these fields, making sure that "Full Name" and "Email" fields will remain uneditable.
Does anyone have any idea how I would do this?
Cheers.