I am learning ruby on rails
but do not understand why some variables have two front points, please explain to me, for example a piece of code either:
class ApplicationController < ActionController::Base
session :session_key => "ruby_cookies"
end
I do not understand, why :session_key => "ruby_cookies" begins with two points,and also seems a hash with that arrow =>
I learned the basics of Ruby, and there was none of this, just know that the have class variables #,:,
another example:
<% # Posts.each do | post |%>
<% = link_to 'Destroy', post, :confirm => 'Are you sure?'
: method => :delete%> </ td>
</ tr>
other[:variable]
<% End%>
why :confirm => 'Are you sure', and other[:variable] begins with two points?,
thanks
they are named symbols, they are different of variables
Related
I am creating a small college project in Ruby on Rails, and I came across a problem: I have a table named Person and another called Tools.
People have many tools and each tool and possessed by a person.
I need to add a feature that allows the loan of tools between people of the system. For this, I created an attribute called 'loan' in the Tools table, which has a value of 0 means that the tool not this borrowed and if it is 1, that this borrowed. In view show the tools, I created a button with the function to borrow the tool. My problem is how do to make this button change the attribute 'loan' from the Tools table from 0 to 1 and then reverse it. Someone would have a solution or a better suggestion?
I would create 2 routes name "barrow" and "retrieve" like this:
#config/routes.rb
get '/tool/:id/barrow' => 'tools#barrow', as: :barrow
get '/tool/:id/retrieve' => 'tools#retrieve', as: :retrieve
Now in your view you can do something like
# tool.index.html.erb
...
<% if tool.loan %>
<%= link_to retrieve_path(tool), 'Retrieve', class: 'btn ...' %>
<% else %>
<%= link_to barrow_path(tool), 'Barrow', class: 'btn ...' %>
<% end %>
...
now you have to create the controller action you have to create the retrieve and barrow methods
#app/controllers/tool_controller.rb
class UserController < ApplicationController
...
def retrieve
#tool = Tool.find_by(params[:tool])
#tool.update_attributes(:loan, 0)
end
def barrow
#tool = Tool.find_by(params[:tool])
#tool.update_attributes(:loan, 1)
end
...
end
I hope that this help you
Probably something really simple but keep getting syntax errors.. I want to add a class to my image_tag, which is using a helper to form part of a url , though didnt think this would effect where i put my class.
<%= image_tag(aws_asset "/assets/img/thumb-1.jpg"), :class => "stock" %>
Can anyone see anything obvious that im missing.
Thanks
Try this:
<%= image_tag(aws_asset("/assets/img/thumb-1.jpg"), :class => "stock") %>
Edit: Your original code was calling the image_tag function with the result of aws_asset "/assets/img/thumb-1.jpg as the only parameter (the :class => "stock" bit was left out). The additional parentheses change call to the image_tag function to have two parameters, the first being the result of aws_asset("/assets/img/thumb-1.jpg") and the second being :class => "stock".
I've jumped into learning Ruby by going straight to Padrino with Haml.
Most of the Padrino documentation assumes a high-level of knowledge of Ruby/Sinatra etc...
I am looking for samples that I can browse to see how things work. One specific scenario is doing a simple form. On my main (index) page I want a "sign up" edit box with button.
#app.rb
...
get :index, :map => "/" do
#user = "test"
haml: index
end
get :signup, :map => "/signup" do
render :haml, "%p email:" + params[:email]
end
...
In my view:
#index.haml
...
#signup
-form_for #user, '/signup', :id => 'signup' do |f|
= f.text_field_block :email
= f.submit_block "Sign up!", :class => 'button'
...
This does not work. The render in (/signup) never does anything.
Note, I know that I need to define my model etc...; but I'm building to to that in my learning.
Instead of just telling me what I'm doing wrong here, what I'd really like is a fairly complete Padrino sample app that uses forms (the blog sample only covers a small part of Padrino's surface area).
Where can I find tons of great Padrino samples? :-)
EDIT
The answer below was helpful in pointing me at more samples. But I'm still not finding any joy with what's wrong with my code above.
I've changed this slightly in my hacking and I'm still not getting the :email param passed correctly:
#index.haml
...
#signup
- form_for :User, url(:signup, :create), :method => 'post' do |f|
= f.text_field_block :email
= f.submit_block "Sign up!"
...
#signup.rb
...
post :create do
#user = User.new(params[:email])
...
end
EDIT Added Model:
#user.rb
class User
include DataMapper::Resource
property :id, Serial
property :name, String
property :email, String
...
end
When this runs, params[:email] is always nil. I've compared this to bunches of other samples and I can't see what the heck I'm doing wrong. Help!
You can browse some example sites here: https://github.com/padrino/padrino-framework/wiki/Projects-using-Padrino
Or you can browse sources of padrinorb.com here: https://github.com/padrino/padrino-web
The best way also is to generate admin: padrino g admin where you should see how forms works.
The tag form perform by default post actions unless you specify :method => :get|:put|:delete so in your controller you must change :get into :post
post :signup, :map => "/signup" do ...
Since you are using form_for :user params are in params[:user] so to get email you need to puts params[:user][:email]
In my rails 3.1 project I have a Book model that has a ID, NAME, and BOOK_ORDER. I am using the ranked-model gem, which for its sorting process creates large numbers in the sorting(:book_order) column. Im looking for some help to create a method to sort all of the Books by the :book_order column, then simplify the :book_order numbers.
So, I have this:
controller
#books = Books.all
view
<% #books.each do |book| %>
<%= book.book_order %>
<% end %>
# book1.book_order => 1231654
# book2.book_order => 9255654
# book3.book_order => 1654
But want this:
view
<% #books.each do |book| %>
<%= book.clean_book_order %>
<% end %>
# book1.clean_book_order => 2
# book2.clean_book_order => 3
# book3.clean_book_order => 1
Additionally, i don’t want to change the database entry, just use its current values to make simpler ones.
Thanks!
UPDATE:
Thanks to nash’s response I was able to find a solution:
In my Book Model I added the clean_book_order method:
class Book < ActiveRecord::Base
include RankedModel
ranks :book_order
def clean_book_order
self.class.where("book_order < ?", book_order).count + 1
end
end
<% #books.each do |book| %>
<%= book.book_order_position %>
<% end %>
EDIT:
Oh, I see. https://github.com/harvesthq/ranked-model/issues/10
I'm working on a Dungeons and Dragons character database. I have two models, character and statistic. I want this to work where each character has one set of statistics. The problem is, when I create a new character, every character shares the same statistic information. This is probably a really easy problem to solve, but I've been butting my head up against it and can't figure it out.
Here's code from the character model:
class Character < ActiveRecord::Base
has_many :statistics, :dependent => :destroy
end
Here's code from the statistic model:
class Statistic < ActiveRecord::Base
belongs_to :character
end
What's the proper code for displaying the statistic model when viewing the character? Do I need to use a link_to or a render tag? Thanks!
Since there is many of them, you'll need to use a loop or something:
<ul>
<% #character.statistics.each do |stat| %>
<li><%= stat %></li>
<% end %>
</ul>
Even better would be to use a partial:
<ul>
<% #character.statistics.each do |stat| %>
<%= render :partial => "characters/statistic", :object => stat %>
<% end %>
</ul>
Then you'll have app/views/characters/_statistic.html.erb:
<li><%= statistic %></li>
This way you can use the code for the statistic rendering in other places too.