Kind of easy question that for a reason i couldnt handle.
on my server side i am saving a hash:
#profile = {"goals" => goals, "won" => result["success"][0]["won"]}
if i am printing to server log console
puts #profile["goals"]
its perfectly printing it.
how can i print the value of goals on the view itself? the following didnt work for me
<%= #profile.goals %>
<%= #profile[goals] %>
<%= #profile{goals} %>
thanks
Use the below :
<%= #profile['goals'] %>
As #profile hash has a key as 'goals'.
Related
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 -->
I have a form that the results just show if render instead of redirect.
so the form must not be redirected. someone know if this is possible with rails?
the form is:
Pac: <%= #pac %>
Sedex: <%= #sedex %>
<%= form_tag calculate_ship_path, :method => "get" do %>
<%= text_field_tag :post_code%>
<% end %>
the order controller and action is:
def calculate_ship
frete = Correios::Frete::Calculador.new cep_origem: "#{#order.seller.post_code}",
:peso => "#{#order.product.weight}",
:comprimento => "#{#order.product.weight}",
:largura => "#{#order.product.weight}",
:altura => "#{#order.product.weight}",
cep_destino: params[:post_code]
servicos = frete.calcular :sedex, :pac
#pac = servicos[:pac].valor
#sedex = servicos[:sedex].valor
render '/path/to/rails/app//orders/:id/checkout'
end
and the routes is:
get '/path/to/rails/app//orders/:id/checkout', to: 'orders#checkout', as: :calculate_ship
Rails has a very handy way to handle this: remote forms. The caveat here is that if you're not using Unobtrusive JS (UJS) then this won't work and you'll have to wire it up the hard way.
In form tag notation it would look something like this, presuming you're using Rails 5:
<%= form_tag(calculate_ship_path, remote: true, method: "GET") do %>
<%# form stuff %>
<% end %>
You should be able to do this with most Rails versions, but it might look a little different. What this does, effectively, is submits your form via AJAX. You'll then be able to bind JS event listeners to ajax:success or ajax:error and handle the response you get from calculate_ship.
One thing to note is that when you're doing form submissions, the method is defaults to POST, and probably should be that, a PUT, or a PATCH.
Here's the related docs for rails: http://guides.rubyonrails.org/working_with_javascript_in_rails.html#form-tag
I got the following lines in my view:
<p><%= #runnings_past.map do |f| %>
<%= f.title %>
<% end %>
</p>
which works just fine in the console --> output:
2.1.2 :076 > #runnings_past.map do |f|
2.1.2 :077 > f.title
2.1.2 :078?> end
=> ["Murtenlauf"]
2.1.2 :079 >
But when I use it in the view as seen above, I get an expression like this:
Murtenlauf ["\n"]
Where does the ["\n"] come from?
You are using the wrong iterator. You want to use each instead of map. Map creates a new array where each element is the result of each iteration in the block. While each simply calls the given block once for each element in the array.
In addition, as pointed out by #JTG, you want to remove the =. You only need = for showing output. You don't need it for logic.
<%= #runnings_past.map do |f| %>
<%= f.title %>
<% end %>
Should actually be
<% #runnings_past.each do |f| %>
<%= f.title %>
<% end %>
Notice the lack of = (in addition, I changed map to each because you're just iterating through the array)
<%= means evaluate the code and insert the result into the html structure. So what you were doing was evaluating the .map and putting the result into your html. So you would iterate through the #runnings_past code, place the f.first into the html (which is what you want) but then when you were done with that, you were placing the result of the mapping (which apparently was an array with a string return character) into the html afterwards.
I have a form below displayed on /parties and would like to take whatever a user inputs in this form and add it to the url. I.e. if they searched for "hello" they would be redirected to parties/hello
<h1>Search for a Party</h1><br>
<%= form_tag({:action => "search"}, {:method => "get"}) do %>
<%= label_tag :q, "Enter Playlist Code:" %>
<%= text_field_tag(:q) %>
<%= submit_tag("Find Playlist", :name => "submit") %>
<% end %>
What is the best way to do this?
The logic would go into the PartyController.
Something like this in (syntax may be a little off):
PartyController.rb
def search
result = params[:q]
redirect_to '/parties/' + result
end
yeenow123 gave you a right way, I just want to clarify some points.
"parties/hello" will confuse the routing with parties/:id (show action)
If you don't want to use show action, you should except this from your route.rb: parties resource or you can use show action as function which has params[:id] is your search key and keep working on this.
I'm referring to Rails 3.2's Data Store feature, in which there's the option to store key-value stores in a textfield, even if you're using a relational database like MySQL...it works fine when programmatically manipulating the fields.
But what documentation is there to update these fields from a RESTful HTML form? Or is this something that's not recommended at all? That is, the better solution would be to go to NoSQL?
If I understand the question, I think you just need to declare the field name holding the store, and the associated accessors (properties) in the model, like
store :settings, accessors: [ :weight, :length, :color ]
at which point the field works with AR and AREL just like any other, even with forms.
There's very little magic here. The field holds a hash of values; the store declaration lets Rails know that you can reference them like something.weight or something.color, whether reading or writing. Simple and slick. Classic DHH.
although the question is quite old someone else might find it useful, also im pretty new in ruby and rails so there might be a better way to do this.
In the model:
#user.rb
attr_accessible :preferences
store :preferences
then in the form partial:
#views/users/_form.rb
<% #user.preferences.each do |k, v| %>
<% form.fields_for :preferences, #user.preferences[k] do |p| %>
<div class="field">
<%= p.label k %>
<br/>
<%= p.text_field k, :value => v %>
</div>
<% end %>
<% end %>
Now to add some extra fields from the form ive created 2 attr_accessor in the model:
attr_accessible ... , :new_pref_key, :new_pref_val
attr_accessor ... , :new_pref_key, :new_pref_val
then added the 2 new fields on the form
<%= f.label :new_pref_key %>
<%= f.text_field :new_pref_key %>
<%= f.label :new_pref_val %>
<%= f.text_field :new_pref_val %>
on my controller i made a function that check the presence of the new fields and then merge the previous values of the prefs with new ones, like this:
#users_controller.rb
...
new_key = params[:user][:preferences][:new_pref_key]
new_val = params[:user][:preferences][:new_pref_val]
new_preference = {
new_key => new_val
}
current_params = params[:user][:preferences].merge! new_preference
...
done that i return it and pass it to the update_attributes, hope it helped!