how to place ejs <%= value %> between <>? - ejs

Essentially I am trying to do this:
To: <%= userName %> <<%= userEmail%>>
But the double brackets seem to make "userEmail" return nothing.
Answer: User #haxxxton had the correct answer of
To: <%= userName %> <<%= userEmail%>>

Related

Using inputs_for with #conn

I am attempting to create a form like this:
<%= form_for #form_object, registration_path(#conn, :register), [class: "register_form"], fn f -> %>
<div class="register_alert">
<%= Web.PartialView.render("flash_alert.html", conn: #conn) %>
<%= inputs_for f, :user, [default: %{name_first: nil}], fn fu -> %>
<%= label fu, :name_first, "FIRST NAME", class: "register_label" %>
<%= text_input fu, :name_first, class: "register_text-field"%>
<% end %>
<%= submit "Sign Up", class: "register_register-button" %>
</div>
<%end%>
That #form_object is a the conn passed in at first because I have no changeset until they enter data (create form). However, I keep getting the oh so helpful 'argument error' crash when trying to run this. If I remove the inputs_for section, everything is fine. How do you use inputs_for with a connection object in phoenix?
Phoenix 1.4.0 being used.
Plug.Conn is a struct containing nearly everything about the connection.
Phoenix.HTML.Form.form_for accepts the connection object as a first argument.
Since you were even of less help than Phoenix providing exactly zero information about the error message, I’d suggest the issue is in a well-formedness of #form_object. You clearly have #conn object there on hand, pass it as the first argument:
<%= form_for #conn, registration_path(#conn, :register) ...

How to DRY two slightly different forms?

I know about partial in rails, but it seems that I can only re-use exactly the same form with partial.
What if I have two forms that differ only in one field? For example, in my classroom app, I have one form for a peer-grader and one form for a teacher-grader.
The teacher's form is as follows. The peer-grader form is exactly the same, minus the grade field. How do I DRY this?
<%= form_for #doc,
url: student_homework_document_path(student_id: #doc.submitter_id,
id: #doc.id),
html: { multipart: true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :grade %>
<%= f.number_field :grade %>
<%= f.label :graded_file %>
<%= f.file_field :graded_file %>
<%= f.label :graded_file_source_code %>
<%= f.file_field :graded_file_source_code %>
<%= f.submit "Submit grading", class: "btn btn-large" %>
<% end %>
I would have thought you had one controller called teachers and one called peers or something. You could keep the form the same but add a variable to the peer-grader method such as #peer_grader = true. Then in your form add:
<% if #peer_grader = true %>
<%= f.label :grade %>
<%= f.number_field :grade %>
<% end %>
You would also need to set another variable called #url in both controllers set to the correct path and then update the form to:
<%= form_for #doc, url: #url, html: { multipart: true } do |f| %>

What is wrong with my coffeescript in Stripe?

I've been working on integrating Stripe into my web application, and it doesn't seem to be working. To help me along, I've been using Ryan Bates's Rails Cast on integrating Stripe. Whenever I try to run the payment form, I get an error saying that "There was a problem with my credit card". I think the problem lies in my coffeescript file, but perhaps I'm wrong. I've included the stripe user token as a part of my user model instead of placing it into its own subscription model. Here is the coffeescript code I have:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
user =
setupForm: ->
$('#new_user').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
user.processCard()
false
else
true
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, user.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 500
$('#user_stripe_card_token').val(response.id)
$('#new_user')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
I'm a beginner when it comes to programming, so any help you can give me would be great.
Here's the error I get in my terminal when I try to sign up:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xas+iA+a3op7jUi57qTr7XWQSClPscA7fR19rkclkEE=", "user"=>{"stripe_card_token"=>"", "name"=>"Jack", "email"=>"email#example.com", "phone_number"=>"203-xxx-xxxx", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('jjets718#yahoo.com') LIMIT 1
Stripe error while creating customer: Invalid token id:
My view for the signup is this:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.hidden_field :stripe_card_token %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :phone_number, "Your cell phone number" %>
<%= f.text_field :phone_number %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password confirmation" %>
<%= f.password_field :password_confirmation %>
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
My code for my controller is this for the create method:
def create
#user = User.new(params[:user])
if #user.save_with_payment
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to edit_user_path(current_user)
UserMailer.welcome_email(#user).deliver
else
render 'new'
end
end
My code for the database migration for the user token is this:
class AddStripeToUsers < ActiveRecord::Migration
def change
add_column :users, :stripe_customer_token, :string
end
end
And the code for the save_with_payment method in my model is this:
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: 1, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
2 things that come to mind:
You should be doing a status check for 200, not 500
You may need to require the coffeescript file in your application.js
e.g. //= require users
I could be wrong, but at this point:
handleStripeResponse: (status, response) ->
if status == 500
$('#user_stripe_card_token').val(response.id)
In addition to changing if status == 500 to if status == 200, this line $('#user_stripe_card_token').val(response.id) may need to be $('#new_user_stripe_card_token').val(response.id). Make sure to check the input ID.

Variants of <% when writing code inside views in MVC2

I'm currently learning MVC2, and I have seen three variants of the tags that contain the actual code for a View:
<% ... %>
With a = after the %
<%= ... %>
and with a : after the %
<%: ... %>
What is the difference of these three code containers?
And are there any other variants of these?
<% ... %>
is just a block of code
<%: "blah blah" %>
Is Shorthand for
<%= Html.Encode("blah blah") %>
Which is shorthand for
<% Response.Write(Html.Encode("blah blah")) %>
1.<% ... %> just block of code
<%if (Model.HelloWorld != null){%>
Hello World!!
<%} %>
2.<%= ... %> plain text without escaping
<%=Model.HelloWorld %>
3.<%: ... %> text with escaping equal <%= Server.HtmlEncode(Model.Something) %> Details here
<%:Model.HelloWorld %>

What is difference between these tags <% <%: <%= in ASP.NET MVC 2?

The title contain my whole question.
<% /* Is a codeblock */ for(int i = 0;i<5;i++) { } %>
<%= "Writes something to the output stream" /* Response.Write */ %>
<%: "HTML-encodes this <b>hello</b> to the output stream" %>
For a good explanation about the <%, <%= and <%# syntax and their usage, please read this article.
The <%: syntax is new in .Net 4 and is used for encoding HTML output. See this article of ScottGu for more information about that.
<% %> is used just to execute server side code
ex. <% if(oject){...} %>
<%= %> is used execute server side code and return value
ex. <%=Html.Encode(Item["Name"]) %>
<%: %> is used execute server side code but it will return Html
Encoded string
ex. <%Item["Name"] %>
Source : What is difference between these tags <%, <%: , and <%= in ASP.NET MVC 2?