Submit a form from different page - ruby-on-rails

I have a Driver form (which is basically the user - I can do current_driver and stuff) and one of the fields is a boolean checkbox for 'subscribe to newsletter'.
I want to put this form on multiple pages that do not share the same controller, i.e Dashboard which has a pages controller.
I am using a partial to do this so i can just add the partial to a template easily.
I believe that the form should post to edit_driver_path as it's the driver form? At the moment when I submit the form, I get "No route matches [POST] "/drivers/dashboard/edit".
Am I on the right path here?
Let me know if I need to post controllers, forms or whatever.

Related

Generic application controller action

I have a rails app that displays user activity on the RHS on each page.
Presently I pass the collection to the partial directly:
<%= render partial: "activities/activity", collection: current_user.activities.order(created_at: :desc) %>
I wish to now paginate this activity list.
current_user.activities.order(created_at: :desc).page(params[:page]).per_page(10)
I am guessing I need to have this set as an instance variable and have it placed in a route that can be accessed from the view.
My question is where should I define this instance variable as the route needs to be generic as the activity is displayed on a views.
If it helps I am doing the pagination with ajax, "remote: true".
I think what you need to do is have a controller action that receives the ajax request which would include the page number. This action would pull up the relevant activities for that page (using the code you put in the question) then it would render some js which would clear the activities panel and repopulate with the new page of actions. It would also have to re-render the pagination controls so that the links are updated with the new page numbers.
If you're using a gem for pagination then it probably has a method to call to generate the pagination controls, and those controls will link to the route to the controller action that I described, passing the relevant page number as a parameter.

Creating a contact form in a Bootstrap modal

I want to have a contact form in a Bootstrap modal. Making a modal pop up was pretty simple.
The email will not go to the admin. The idea with my app is sort of like Airbnb, where users can have a house (one-to-one in my case). So on a show house page, a user can click a button to contact the owner. I can get the email from the relationship between the house and the user.
My contact form should not save to the database, just send an email. The only field I need is the message. I should be able to get the email from controller of the page serving the modal (that's why I don't want to introduce another controller).
There are some tutorials on making a contact form, such as this one: https://rubyonrailshelp.wordpress.com/2014/01/08/rails-4-simple-form-and-mail-form-to-make-contact-form/
But that creates a contact model, view, and controller. Right now my modal is a partial. How can I make that partial be the view for the contacts?
I feel like I shouldn't need a new model, view, and controller. Couldn't I just do this with a new mailer?
My question was probably dumb, due to my lack of understanding of Rails. I figured it out, and here is what I did, in order to help others.
The fact the form is in a modal does not matter. It's easy to render a form in a modal.
Because when you push the submit button, the form has to do something, a controller and route is needed. Then I needed a mailer. Basically this is the answer, but I did not need another view, since the form is already in my modal:
Contact Form Mailer in Rails 4
For your action of controller create a js.erb file like contact.js.erb and render your form partial in that. For example
jQuery("#modal_pops").html("<%= j render 'your_modal' %>");
jQuery("#model_container").modal("show");
and in that modal write your form. If you dont want to save that the information to database then just submit the form to an action and send email from that action. I hope you get it.

Rails - Reload action with new value in params

I have the following relation in my model:
A user can have several consumers
According to that, I call several actions from different controllers that depend on that consumer_id in the URL. So, I do stuff like:
/:consumer_id/products/all
/:consumer_id/locals/all?params...
I want to be able to given that I am in a particular view, let's say /3/products/all, be able to refresh, redirect or whatever is the best to /4/products/all, with a form that shows the different consumers attached to that user.
I know how to display the form, but I fail to see which action should I put given that I want to load the current controller, current action and the current params, except that I want to change the params[:consumer_id] and the session[:consumer_id] to the one chosen by the user in the form of the view.
What's the best or appropriate way of doing so?
This is a link to the same page with the same parameters and one additional parameter:
<%= link_to "Show more results", url_for(params.merge(:per => 100)) %>
If you want to modify a parameter rather than add a new one, you can modify the params hash just as you would modify any hash, and use url_for again.
Inside the view you can access both the current controller using controller_name and the current action using action_name
Your question is not clear. You want to choose consumer_id from your form, then use that consumer id inside the same page or for links to other pages?
If you want to use the consumer id for the current page, you need to use javascript or JQuery to update all attributes of the current page.
If you want to use that consumer id for links,
you may also use JQuery to update all links on your page
Or, you can submit the form to the server with new consumer_id.

Ruby form submission

I am new to ruby on rails.
I have a form say form1.On clicking the submit button this form should take me to another form with the parameters passed.Please let me know how to do it and also how to access the form1 parameters in form2.
If you mean a multistep form, check out: http://railscasts.com/episodes/217-multistep-forms
The basic concept is that you will have to set the url to post to in your form tag (the default is a create or update action). Once in your new controller action, your previous form is available in the params hash. Without more details on your question, we can't provide better details.

Render another controllers view as partial (Rails 3)

Sorry, only have been using rails for 10 day and I'm completely lost. Have a post model/controller and comment model/controller.
In post/show, I want to click an ajax button and replace a div (#addcomment) with the partial generated from comment/new (The new action takes two params and builds a comment for an ajax form partial). The comment/new partial is an add comment form with remote => true.
Anyone willing to point me in the right direction?
Tried using an ajax button in the view, that calls an action that replaces the div with the comment/new but it didn't work.

Resources