0

For the information .*js.erb is loaded when respective controller action take place.

I know the different version of this question is asked, so anyone having ability to flag this as duplicate will be quick to do it. I won't have any problem with that as long as I get the answer to my specific problem.

Problem is a s follows:-

When user is being is created/updated it goes through four action in controller, for creation new and create(if validation fails same partial is rendered with error message coming from create action) and to update edit and update, which have different partial serving the purpose. Although, ideally one partial should be enough but requirement differ for new and edit so... two, and keeping one will require lot of conditional statement ultimately leading it unreadability of code. Now, I have .*.erb.js file which is same for both the partials, and is required only when these page is requested. This leads to redundancy in code which is not good for maintainability of code moreover would like avoid putting js code in html file. For these two reasons, I would prefer a better solution to my problem. To state my trouble in exact sense I have listed couple of examples below:

@user, is initialized in respective action of controller and phone_number_home and phone_number_general of user is validated. so in js.erb I have

<% if(@user.phone_number_home.present? ||  @user.phone_number_general.present?) %> 
  <%= do sth %>
<% else %>
   <%= do sth %>
<% end %>

or something like:-

<% if(params[:action].to_s == "edit")%> 
  <%= do sth %>
<% else %>
  <%= do sth %>
<% end %>

error report- in former says undefined method *phone_number_home* for nil:NilClass and in latter it is undefined local variable or methodparams'`, whereas same piece of code under script tag in views of _form_new.html.erb will work just ok. But not when it is kept in asset pipeline(which was the first trouble I faced while seeking my answer and after a lot googling this link sorted the problem out but only partially and ramifying my trouble into above one, so once again lot of googling got me multiple reference for solution, such as

1:) NoMethodError thrown when trying to use controller variables in js.erb file

2:) Even went over this screen-cast over and over again in case missed something

but none of them serve the purpose, besides above problem is just an example of what I should be able to do in with embedded javascript, to accentuate my problem I am also rendering a partial onClick() of html tag, where I send different initialized variable in controller action so all these trouble for seeking an answer to my problem despite having workaround( put the code in respective partial _.*.html.erb) to my trouble but to escape redundancy and strengthen maintainability of code, I need a better solution to my problem here.

Thanks in advance.

Community
  • 1
  • 1
zeal
  • 465
  • 2
  • 11
  • 22

1 Answers1

0

Note:- Any one reading this solution won't get answer of or(second) part of the question but solution provided here will surely be of great help in future, if it does not answer the question which lead you here.

After posting the question I was frantically coming back to page see if at least I got a comment on my question, or flagging as duplicate(so that I get the answer for what I have been toiling for very long), but of no avail. But at same time I tried different approach to solve the problem from the Knowledge I gained in last couple of days in wake of my uneasiness regarding the same. And with some frantic thoughts to solve the problem there came a moment of epiphany, when I got the idea from - rendering html partial (used ubiquitously) in rails project. So I thought why not try same for javascript file. Hence, the below code I wrote is this in partial(html) from, where under partial javascript is rendered in script tag :

<script type="text/javascript">
    <%= render '_new_edit' %>
</script>

While I was trying this I got an error, because file name was wrongly in-putted, and error I got in console was this:

# other details, processing and query to database
Completed 500 Internal Server Error in 24.5ms

ActionView::MissingTemplate - Missing partial users/_new_edit, application/_new_edit 
with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. 

Searched in:

  *"/app/views"
  * "~/.rvm/gems/ruby-2.0.0/bundler/gems/sms_confirmable-785b1781433c/app/views"
  * "~/.rvm/gems/ruby-2.0.0/gems/devise/app/views"

Note:- The above request request was through ajax to new action in users controller for pop-up form(important for final solution).

so when file name was corrected(by removing '_' in front to input 'new_edit' as argument to render method.) voila- it worked and javascript loaded smoothly. And I thought I have solution so went ahead to create a user which ran successfully. Now it was time to create user with wrong inputs or less inputs so that validation fails but this time it was not a happy face. An error was flagged and report read this:

# user details in params, processing by users controller create action and required query to db
Completed 500 Internal Server Error in 24.5ms

ActionView::MissingTemplate - Missing partial users/_new_edit, application/_new_edit 
with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. 

Searched in:

  *"/app/views"
  * "~/.rvm/gems/ruby-2.0.0/bundler/gems/sms_confirmable-785b1781433c/app/views"
  * "~/.rvm/gems/ruby-2.0.0/gems/devise/app/views"

Note:- As form submission was a non ajax request and there was error in validation of the form so user of the site is supposed to be presented with error report of the form inputs which required rendering of same partial form so while parsing of same page rails looked for 'new_edit' file with format => [:html] which does not exist in project so the above error.

As one can now see that different format of partial was looked for different type of request(ajax, or simple html link). Although in former it looked for both([:js, :html]) type in partial but in latter in only looked for :html format because request was through html link.

So from here I got an idea why not provide formats args to render method, and doing the same worked. Though there is one caution while giving arguments to render- when not in default case then it is better to explicitly provide partial as shown below.

<script type="text/javascript">
  <%= render(:partial => 'new_edit', :formats => :js) %>
</script>          

not(XXXXXXXXXXXXXXXXXX)

 <script type="text/javascript">
   <%= render('new_edit', :formats => :js) %>
 </script>   

as this generate error when rendering is not through ajax.

Note:- Do not try this method without script tag as that would just write js code and any content of html partial being rendered in js file.

Caveat:- As I haven't got any literature on the points I mentioned so anyone having better understanding is welcome to improve answer or embed this answer with links supporting the answer or otherwise.

cheers!!!

zeal
  • 465
  • 2
  • 11
  • 22