Why a ruby command runs even if a user don't activate the script yet? -
i'm ruby user, trying make web service receives user's active request. made button, of class "btn-send-alert". after html code, put script function.
<div class="page-title"> <button class="btn-send-alert" style="background-color: transparent;">help request</button> <p>hello</p><br> </div>
........
<script> $(".btn-send-alert").click(function(){ alert('hello!'); <% smalier.class_alert(@lesson,current_user).deliver_now %> }); </script>
the problem is, ruby code start on own before click button.
and if click button, no email delivered longer.
maybe in point, think i'm wrong can't find is. there way can make function work correctly?
looking forward seeing response!
best
thanks rich, able write code works fine! below code code.
<%= content_tag :div, class: "page-title" %> <%= button_to "help request", support_path, method: :get, remote: true, class:"btn btn-danger", params: { lesson_id: @lesson.id, user_id: current_user.id} %> <%= content_tag :i, "wow!" %>
////
def support
@lesson = lesson.find_by(:id => params[:lesson_id]) current_user = user.find_by(:id => params[:user_id])
mailer.class_alert(@lesson,current_user).deliver_now
end
above code runs well!
i'm ruby user
welcome rails!!
stateless
firstly, need understand rails applications - virtue of running through http - stateless, meaning "state" such user
or account
have re-established each new action.
in short, means invoking actions/commands on system have done through ajax or form of server-connectivity.
many native developers (native apps stateful) don't understand how rails / web apps able retain "state", , make bunch of mistakes code.
receives user's active request
even if understand how set authentication inside rails app, it's important understand virtues of being stateless... eg above line means have have user signed in , authenticated before can send request.
this forms 1 part of problem (i'll explain in second)
erb
secondly, other problem have erb nature of rails.
the ruby code start on own before click button.
this happens because you're including pure ruby code in front-end scripts. means whenever these scripts loaded (triggered), fire.
the bottom line here need put script on server. otherwise run...
fixes
1. erb
<%= content_tag :div, class: "page-title" %> <%= button_tag "help request", class:"btn-send-alert" %> <%= content_tag :p, "hello %> <% end %>
you'll thank me in 1+ months.
convention on configuration means use many of rails helpers can. don't need go stupid it, more "conventional" code is, better future developers improve it.
another tip - only use html formatting; css styling. don't use
<br>
unless actually want break line.another tip - never use inline styling - rails has adequate asset pipeline should put css
--
2. ajax
secondly, use of javascript incorrect.
more specifically, you're calling server-based function inside front-end views. explain little more, i'll show famed mvc image post on here lot:
this how rails works (mvc - model view controller) - means whenever deal application, have accommodate layer of abstraction between user & app -- browser.
by nature, browser not stateful - stores session cookies have authenticate on server. cannot call "server" code in front-end html/js.
this why ruby code firing without interaction, although i'm not sure how it's able fire in asset pipeline.
if want make work properly, you'll need create controller action invoke mailer send function, you'll able using following setup:
#config/routes.rb :support, to: "application#support", as: :support -> url.com/support #app/controllers/application_controller.rb class applicationcontroller < actioncontroller::base respond_to :js, only: :support def support smalier.class_alert(@lesson.current_user).deliver_now end end #app/views/controller/view.html.erb <%= content_tag :div, class: "page-title" %> <%= button_to "help request", support_path, method: :get, class:"btn-send-alert" %> <%= content_tag :p, "hello" %> <% end %> #app/views/application/support.js.erb alert ("hello");
Comments
Post a Comment