Unable to use css classes in Ruby on Rails project - html

I'm using a codepen frontend template in my Ruby on Rails project so now i'm unable to connect my css file with my login.html.erb, like in this code, for submit button i want to use the "submit" class which i have styled in my css file. Also the final rendered page i not stylised which brought me to the conclusion that css file is not getting connected. Please be precise on your answer as I'm a complete beginner as this is my first RoR project.
Codepen Template
and My Rails Project and My login.html.erb file.
<h2>Welcome back,</h2>
<%= form_tag("/create_session", method: :get) do %>
Username :<%= text_field_tag(:username) %><br>
Password :<%= password_field_tag(:password) %><br>
<%= submit_tag(:Login), class => "submit" %>
<% end %>
I'm getting the following error:-
app/views/sessions/login.html.erb:22: syntax error, unexpected ',', expecting ')'

If you want to declare a css class for the submit_tag you can do it using :class => "class_name", or better if it would be like class: "class_name".
So you could try with:
<%= submit_tag(:Login), :class => "submit" %>
<%= submit_tag 'Login', class: "submit" %> <!-- recommended way -->

Related

How to stop Rails app crashing due to form_tag

I have an issue with my Rails App which just doesn't make sense. I have a form_tag so that the user can select a date, which is then passed to the controller. All I need it to do is reload the page with the selected date in the URL as params. This same code works elsewhere in my application, but in this instance it crashes the app.
The app completely freezes in both development and production (Heroku). I cannot figure out why.
Here is the form:
<%= form_tag new_reservation_path, method: :get do %>
<%= date_field_tag 'reservation_date', params[:reservation_date], class: "button-standard", style: "display: inline-block;", placeholder: "DD-MM-YYYY" %>
<div class="spacer"></div>
<%= hidden_field_tag(:room, params[:room]) %>
<%= submit_tag 'next', class: "button-standard", style: "display: inline-block;" %>
<br>
<br>
<% end %>```

html/rails form. Input

This is a simple question but I can't seem to find a solid answer. I simply want to know if this is valid? it's a basic form_for with in input at the bottom.
## Form
<%= form_for #snitch, html: { class: "form-actions", method: 'delete' } do |form| %>
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span>
<button type="submit" class="button button--modal delete-snitch" data-snitch-id="<% #snitch.token %>" value="Yes, delete it.">
<% end %>
Is the third line valid? specifically where it says data-snitch-id="<% #snitch.token %>"? if it is not. can someone help me figure out how I can do something like that?
HTML data attributes are perfectly valid and widely supported. They're used to store custom data in an element. You can create elements with those attributes in rails helpers as well.
<%= button_tag "Yes, delete it.", type: :submit,
data: {"snitch-id" => #snitch.token},
class: 'button button--modal delete-snitch' %>
The only problem with your example is that you're not printing the value of #snitch.token. You should be using <%= #snitch.token %> instead of <% #snitch.token %>
Use #{ } expression instead.
data-snitch-id="#{#snitch.token}"

Why does my signup form in rails no longer work when this small snippet of code is added to it?

Originally, for a signup form on my site, I had this code for the beginning of the form:
<%= form_for(#user) do |f| %>
When I add this html id in the following code for the beginning of my form:
<%= form_for(#user, :html => { :id => 'payment-form' }) do |f| %>
The form no longer works.
Is there a way to still add the html id I specified above to my form while allowing my form to work? How can I go about doing that? Why doesn't the form actually process when I hit 'submit' after I add the html id.
Thanks so much!
Try <%= form_for(#user, :id => 'payment-form' ) do |f| %>

Inserting rails into HTML file

When writing an HTML file, why use <%= INSERT RAILS HERE %> vs. <% INSERT RAILS HERE %>
<%= %> emits a string, <% %> runs code.
On the pedantic side, you're writing an ERb template, not an HTML file--the syntax is the same whether it's a template for HTML, JS, or whatever.
The ERB docs provide additional (but not complete) information.
<%= %> will return value and display in your page. Assume that you have person.name = 'Dark'
<%= person.name %>
will display Dark in your web page.
<% %> will not return any value to your page. It just embed simple ruby code. Usually used with `control statement'.
<% if person.present? %>
<span><%= person.name %></span>
<% end %>
When we use <%= %> it simply displays the value returned, on the html page.
<% %> executed the code but doesn't dispaly it on the html page.

Placing link at top

How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?