Closed AnaYs closed 8 years ago
Problem with yielding an html partial through a js script Missing template error when html is supposed to be rendered via js.erb file #new.js.erb: $('#pop-up').html('#{escape_javascript render("new")}'); $('#pop-up').modal();
contact-form is in #_new.html.erb
http://stackoverflow.com/questions/9735866/rails-rendering-the-js-erb-template-using-the-controller http://stackoverflow.com/questions/22940454/ajax-form-and-rails-render-on-js-template
basis for mailer: https://rubyonrailshelp.wordpress.com/2014/01/08/rails-4-simple-form-and-mail-form-to-make-contact-form/
The JS code calls for events on the #pop-up
css ID but I can't seem to find it in the codebase, maybe linking it to the modal ID could fix that
How come you chose to go for a JS approach? Maybe I don't see the big picture of the feature but I think it's more simple to make with plain old html from the source you referenced above
$('#pop-up').html('#{escape_javascript render("**modelname/new**")}'); $('#pop-up').modal();
I think it also requires to specify the model name
Assuming you chose for a JS approach; I made a feature that has some similarities that may give some insight.
The goal was having the follow and unfollow users
Follow button:
`<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>`
The button toggle follow/unfollow:
`$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
$("#followers").html('<%= @user.followers.count %>');`
The follow form rendering:
`<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
`
Formats:
` def create
@user = User.find(params[:followed_id])
current_user.follow(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
`