how to display the content of session[:intented_url] using rails 4 - ruby-on-rails

I want to display the page that the user is logging in for ...
A user has to login before viewing pages. If not, then the user is redirected to login. It is at this point I want to display the name of the page they want to go to and view once logged in.
In the session controller, under the create def - I created the following instant variable:
#intended_url = Session::get('url.intended', url('/'));
and the view page contains the following:
<h4>Sign in to go back to <%= #intended_url %>.
Unfortunately, nothing is showing up?

If you want to store anything in the session, you must do so in a controller that has access to it. This means that setting the session takes place in a controller that is for a page. You do so by
session[:page_name = #intended_id
Then you can retrieve it with
display ------> session[:page_name]
Your problem can also be solved by instead calling the controller from the page.
Controller.controller_action
This displays your controllers action name which if you follow Ruby On Rails design will be your page name.

Related

Rails: Run a method when user left the edit view through the browser back button

Is there a possibility to detect if a user has exited the edit view (edit action) without saving through the back button of the browser and then run a method to set the value of a field in the database? I tried after_action on the edit action but this fires directly after rendering the edit view.
Hitting the back button in a web browser won't involve Rails, but rendering the component to which it is being sent will.
You can save the data of the input fields in the browser's local storage or cookies and then have a before_action: in the controller they were sent to with the back button that looks for the presence of such value.
( This solution involves some javascript )
Then, you can query accordingly to determine if those values should be saved in the database.
Note: If you need to cover not only the back button scenario, but also any other controller to which the user might navigate to in your app, you may add the before_action to somewhere else, like application_controller.

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.

Show / hide component based on page I'm in

I need to show, or hide a search component in my JSF2 application based on the page the user is on. I'm trying to do this by setting a "fromPage" attribute into the session, for example like this:
session.setAttribute("fromPage", "aboutUs");
These attributes are being set whenever the user clicks on a link in the homepage, and I check for them while serving the site's pages. It works well when I use h:commandLinks.
But when I change to h:link this stops working because apparently all the "outcome" methods of h:links on a page are getting called when the page is rendered, and so the "fromPage" attribute always represents the last "outcome" method called -- and it's useless for my purpose.
So, is there any other way to know which page is the user clicking to? And which page am I on?
Use UIViewRoot#getViewId() to learn which view is currently been opened. The current UIViewRoot is available by the implicit EL variable #{view}.
<p>Hi user, you're on #{view.viewId}.</p>
<h:panelGroup rendered="#{view.viewId == '/aboutUs.xhtml'}">
<p>This will be rendered when the current view is aboutUs.xhtml.</p>
</h:panelGroup>

ActionLink back button

I have an Index method that does double duty as showing a list of posts and a queried list of posts and can also have pages so you get urls like /News/Page/1 or /News?query=test
When a user clicks through to a post at say News/Details/1 they get a simple ActionLink that takes them back to the list. BUT I want this link to take them back to the actual page they were on in terms of the paging or the query. How could I do this? I don't want to use the JavaScript history method. Here is my current ActionLink: <%=Html.ActionLink("<< Back to News List", "Index")%> and this is an example of the paging links: <%= Html.RouteLink("<< First Page", "NewsPaging", new { query = ViewData["query"], page = 0 })%>
Thanks
It would probably be easier to just use javascript to send them back to the last page in their history (without creating a link to the specific page).
history.go(-1)
I will go for a ActionLink like
<%=Html.ActionLink("<< Back to News List", "Index")%>
because user can access News/Details/1 directly and if then you have a link that uses history.go(-1) or history.back() function it will not redirect it to the Index action
Another way to do this is to create the link in the controller and assign it to a property of the model or a simple ViewBag variable (up to you). For this example, I'm just going to use ViewBag.
So in your example, going from /News/Details/1 back to /News/Page/1?some-querystring, you can do the following:
In your controller (assuming '1' is the 'id'):
ViewBag.BackButton = String.Format("/News/Page/{0}{1}", id.ToString(), Request.Url.Query);
In your view:
< Back
One draw back to this is that you must know what the previous page was/could be. In your case, you want your back button to go back to your list, so this technique should be fine.

Resources