QR code link to a modal - ruby-on-rails

Is it possible to create a QR code that redirects the user to a modal in a specific page?
As in, you send him to the page X but already trigger click <%= link_to "modaltoshow %> , and the user sees the modal immediately after scanning.
Thanks

If your question is simply "can I launch a page with a modal already open," the answer is yes. You'd just need a javascript that launches it on load, like the selected answer here.
Assuming you need some ruby logic, you could simply change the target of that script with embedded ruby based upon a parameter the QR code passes (for instance, if you have a parameter "target_modal" that is the target modal's id, just reference that in the script). It could look something like this in your script block:
<!-- The if statement just checks for the parameter. If the parameter will
always be present, you may remove this. -->
<% if params(:target_modal) %>
$(window).load(function(){
$('<%= params(:target_modal) %>').modal('show');
});
<% end %>

Related

doing a updated edit form

I am doing a module using modal windows, where in the index I am showing all the records, saving data, editing and deleting everything using ajax. Pressing the respective buttons in the row of each record opens the modal window depending on the button. But I would like to press the "edit" button to send the log id to the controller, search the data and return it in the modal window. To send the id to the controller should I place a form on each button?
A traditional non-AJAX edit button that you would typically see in a Rails index page would look something like this:
<td><%= link_to 'Edit', edit_thing_path(thing) %></td>
You would need to do three things to AJAX-ify this:
change the link_to so that it sends an AJAX request instead,
get the things_controller#edit method to respond with JSON,
write some JS that picks up the JSON response from the server, and populates it in the modal's form.
An alternative approach, which might be easier, would be to use UJS as recommended by the Rails core team. In this case:
change the link_to to request a JS response
change the controller's edit method to respond by rendering a JS file
build a JS file that renders the modal server, side, and then replaces the current modal in your HTML with the newly-built modal form, and then
reveal the modal in the page
Have a look here in the Rails Guides (for Rails v4.2).
To do that I really like to use Best In place its supper simple to use and implement.
there is also a video on how to use it, its a bit old 302-in-place-editing

How to pass button value from view to controller without using form

I am trying to figure out how can I use the button value from view to corresponding controller. I have three button which I have grouped them. Based on user selection, I want to do different functions (eg: List the task, form to add the tasks..etc). For that I want to pass the button value to the controller.
<div class="btn-group btn-group-justified">
Task Lists
Task Category
Add Task
</div>
I am not sure if there is better way to do this than button groups. I am not familiar with javascript also.
To make it clear, I have user information on the left side of the page which is already designed. These buttons are on the top of the right side of the page. Upon selecting the buttons, right side of the page should change/reloaded based on the selection.
You can try following
<div class="btn-group btn-group-justified">
Task Lists
Task Category
Add Task
</div>
You can use links to go to the controller action you want (if I understand your question correctly). You would use the paths/routes to the specific action you want. It would look something like this:
Task Lists
Or you can do it in rails direction, using the link_to method:
<%= link_to "Task Lists", tasks_lists_path, class: "btn btn-primary" %>
Again, this depends what the routes you are using are. If you want to pass parameters with the link, it gets a bit more complicated, but not much.
For more information: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
One option is triggering an ajax post request when the button is clicked and send the data to a particular controller action.
Could also just pass params through a link_to.

Rails: Set a temporary variable?

I need to set a temporary variable (actually more of a "true" or "false") for a view.
The use case is that when a user is created, they are redirected to a dashboard page. For Google AdWords conversion tracking, there is a bit of code that needs to be displayed in the view, but it only should be displayed after the create method has been run.
So I'm guessing the way to solve that is to set some temp variable (#show_conversion or something) and set it for one view (similar to a flash message).
So, how do I do that?
<% unless #user.new_record? %>
<!-- do stuff in here -->
<% end %>

Rails 3.0 . How to pass the select box value through link_to

I knew that Rails3 no longer supporting link_to_remote.... instead of that we can modify
the link_to method by using different syntax as follows :
link_to "Send",{:action =>"send_mail",:question_id =>question.id},:remote => true,:class =>"button small"
My problem is, in my view i keep the select box which contains the list of user's name near by send link button (which user the above syntax)... i want to pass the selection box value to link_to when user click the send button
Here is my View code :
"send_mail",:question_id =>question.id,:user_value
=>encodeURIComponent($F('user_id'))},:remote => true,:class =>"button small" %>
I don't know how to achieve this ....can any one please suggest me on this.
edit: ok now is see your last comment... never mind
What I got from the question, you are looking for something like this:
http://marcgrabanski.com/articles/jquery-select-list-values
It's not really a Rails problem since you can't change the Ruby code from within the rendered HTML (at least that would be very risky if it's possible). The code from above can be easily changed to your needs so that the user gets redirected to the URL that matches the button value.

can link_to lead to rendering sth?

i want to render a partial within a view. so that when button MORE is clicked everything stays the same just additional characters are shown. in my case the whole article.
<%= #article1.content[0..300] + "..." %>
<%= link_to "more", ....... %>
i dont know what the right methot would be. somehow i have to explain to rails that when button more is clicked it shows me the whole article. maybe i shouldn't use method link_to ..
thank you in advance for your replys
What you're looking for is link_to_remote or link_to_function.
link_to_remote will be fetching the rest of the article from your controller and replacing/appending to a DOM element with a partial via RJS. This allows you to minimize unnecessary data being sent, and facilitates handling users that have javascript disabled.
With link_to_function, the entire article will be served when the page is loaded, but the everything beyond the first 300 characters will be hidden by CSS. This is easier to set up but sends a lot more data, it also relies on the user having javascript enabled.
Without looking at the source the average user probably couldn't distinguish between the two methods.
Which choice you go with is up to you. Sorry, I haven't got time to provide code examples, but the internet is full of them.
try link_to_function, use truncate for part and insert hidden tag with full text, switch them using javascript in link_to_function

Resources