I'm just trying to resolve a dilemma of Partials and Helpers in RubyOnRails 3.2.8. I'm new and yes, i tried to find, but every answer is not clear or old more than 2 years. So what's the deal?
I wonder what is best to use for this snippet. Helper or partial ?
<td><%= User.where("id = ?", job.created_by_id).first.email %></td>
<td><%= job.document.to_s.from(53) %></td>
<td><%= job.translation_length %></td>
<td><%= job.translation_language %></td>
<td><%= job.cost %></td>
<td><%= job.translation_type %></td>
<td><%= job.comments %></td>
<td>
<% if job.as_soon_as_possible.to_i.equal?(0) %>
<%= job.due_to_date %>
<% else %>
ASAP
<% end %>
</td>
<td><%= job.status %></td>
I have used both, both work fine, but i'm not sure, if it's good hold it in partial. What about performance between rendering partial and calling helper method ? Read something about it 2 yrs old, and helpers seemed to be way faster. Is there any improvement of rendering partials?
Second, I read about using partials and helpers, but there are many opinions of developers. Am I right : Helpers are using with snippets containing more ruby code with html and Partials with html and small amount of ruby code. (According to this idea, my code above should be placed to helper)?
I think, that helper methods are for specific proposes, they provide uniform and centralized generation of routes, and allows to avoid building some repeating html structures (in conjunction or not with model objects). And a partial is a part of the page, some semantic independent block.
Consider your code.Now you probably listing jobs ,suppose these piece of code repeat more than one places you create partial,like adding/updating record has same fields so create form partial
Now,below code will return job due date or ASAP
<td>
<% if job.as_soon_as_possible.to_i.equal?(0) %>
<%= job.due_to_date %>
<% else %>
ASAP
<% end %>
</td>
So creating helper which will return due date or ASAP depending upon need.
There are many more difference between partial and helper.May be if i found some useful articles i will share with you.
Related
<h1>Listing categories</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Thumburl</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= category.thumburl %></td>
<td><%= link_to 'Show', category_path(category) %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'New Category', new_category_path %>
So, as I said in my last post, I'm trying to go through Ruby code as I find it on Codecademy's RoR tutorial. Since they have such a straightforward tutorial, I figure it would be ridiculous to not understand everything they throw at me.
In this bit, they have an html.erb file that is supposed to form a tabled-view. Everything seems clear to me until they get to <tbody>.
Here, first, we are iterating on an instance variable #categories...now, where this instance variable is found, I don't know. I thought instance variables meant they could only be used within a particular class, yet this is a markup page, i.e. no classes have been defined, so how does Ruby know where or what this instance variable is?
Next: so, as I understand it, the word 'category', here, is serving as a variable because it was specified as such by pipe syntax two lines earlier. Now, whether this variable can be used beyond this html.erb page is beyond me, cause I thought instance variables were denoted with an '#.' With this variable, we call the method 'name'. So, is 'name' a custom method, and if so, where is it defined?
Now, the third <td> tag contains code that tells Ruby to link to some name 'Show,' yet I'm not sure what this name points to...also, 'category_path' is a method we defined in our routes.rb file, and it has the parameter of our blocked variable 'category.'
Lastly, the final row cell links to some name 'Destroy,' but this comma syntax is something I haven't seen before:
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
The instance variable #categories should be defined in CategoriesController, which is a subclass of action controller. Ruby's instance variables are not persistent, and any #variables you defined in a controller action is accessible in the action's view. I am not sure if you are familiar with MVC patterns, and if not, you can take a look at this question:
What is MVC in Ruby on Rails?
Name is an attribute of the resource/model category. You should see its class in app/model/category.rb. It is a subclass of ActiveRecord, which acts as a wrapper of the database of your choice. So you probably have a database table called categories with column name 'name'.
And as #JoelL mentioned, the link_to are view helpers that are meant to simplify your syntax in view files,make your code more organized, and save you time. You can define your own view helpers in app/helpers. Some of the helpers are automatically made for you when you define a resources :resource_name in config/routes.rb. In this case, you probably have resources :categories in your config/routes.rb
In the Rails view users/index.html.erb
I iterated over each users to list them all.
Want sequence no. in front of the user list. I have used each_with_index to achieve this.
I want to know if rails provides any better alternate to this. I am using rails 4.1
<% #users.each_with_index do |user,count| %>
<tr>
<td><%= count+1 -%></td>
<td><%= user.name %></td>
I have created a partial for a LineItem that, in my case, emits a <tr> with a couple of <td>s. This works just fine and dandy.
What I'd want to do then, is use the same partial in different places. The original view has plenty of room, so displaying every cell is ok. But another (a sidebar), has much less space, and I'd only want to display two of the 5 cells.
So the question, then, is what is the correct way of implementing this? I'd like for the "full" version to be the default unless otherwise specified, and only use the limited version if a parameter is passed.
Currently I'd probably use the feature of passing locals to a partial and check if a parameter limited is defined, and if so, skip the last N cells. Am I on a right track, and if so, what kind of a variable should I use (can I use symbols for this)?
Current _line_item.html.erb (that I'm somewhat unhappy with)
<tr>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price, unit: '€') %></td>
<% unless defined? limited %>
<td class="remove_item"><%= button_to 'Remove', line_item, method: :delete %></td>
<% end %>
</tr>
Edit: Added current version.
Yes, you are on the right track. You can do something like
<%= render 'partial_name', limited: true %>
and then in the partial:
<% limited ||= false %> # More explicit as to what default value is
<tr>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price, unit: '€') %></td>
<% unless limited %>
<td class="remove_item"><%= button_to 'Remove', line_item, method: :delete %></td>
<% end %>
</tr>
I'm still new to Rails, and can't seem to wrap my head around this.
I have table showing of several Products with several attributes in form columns. In my index view I'm showing a shortened table, with just some of the columns. In my products#show I'm telling the full story, but obviously only for the selected Product. What I want to do, is letting users select a number of Products in products#index, and then store the selected IDs in a hash, sending them to another view, which I want the IDs to be presented in a full scale table, telling the whole story, of several products at once. Basically this is for comparing Products.
I'm not sure how to tackle this. As I'm already using DataTables, using javascript would make sense, but I'm unsure on how. And ideas? I've tried several ideas, but none of them got me anywhere.
products#index
<table id="products"cellpadding="0" cellspacing="0" border="0" class="table.responsive">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>TG</th>
<th>TD</th>
<th>DK</th>
<th>DF</th>
<th>T260</th>
<th>T288</th>
<th>CTI</th>
<th>Strength</th>
<th>Rigidity</th>
<th>Standards</th>
<th>Manufactorer</th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%= link_to product.name, product %></td>
<td><%= product.TG %></td>
<td><%= product.TD %></td>
<td><%= product.DK %></td>
<td><%= product.DF %></td>
<td><%= product.T260 %></td>
<td><%= product.T288 %></td>
<td><%= product.CTI %></td>
<td><%= product.ElectricStrength %></td>
<td><%= product.rigidity %></td>
<td><% product.standards.each do |standard| %>
<%= link_to standard.name, standard %>,
<% end %> </td>
<td><%= product.producer %></td>
<% end %>
</tr>
</tbody>
</table>
If anyone could spare some time to point me in the right direction, that would be wonderful!
This shouldn't be too tough to tackle, I'll walk you through the basic steps that I would take.
My basic approach would be to take a form of said checkboxes and submit it to an action designed to handle them in the way described, like so:
Create a new route to use for all of this. Take a look at 2.10.2 Adding Collection Routes in the docs (assuming you are using resources). Maybe call it something like "compare".
Use a form not backed by a model to hold your checkboxes and labels (1 of each for each product) - it should post to your new route.
In your controller, add the action for your route. This action should fetch the ids passed in by the form from the params and load the relevant products (remember that find can take an array of ids, so it should be pretty easy).
Finally, add a view for your new action, with the relevant logic to present your comparison.
You can easily come in an backfill this with javascript, but it is not required. Honestly, it would be more useful for adding a little extra panache, like disabling the button and showing a spinner, or something, than a full-on javascript submission, which doesn't really buy you anything in this particular case.
Learning Ruby and Rails. In following the getting started guide if I invoke
rails generate scaffold Post name:string title:string content:text
it generates among other things code like the following in index.html.erb:
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
My only concern in the above is edit_post_path, and my question is, what is it - and specifically from a Ruby standpoint. It certainly has every appearance of being a Ruby method, and its embedded in other code which is most definitely Ruby: posts.each do |post|...end that's all Ruby
But if edit_post_path is a Ruby method, where is the code for it? 'post' is a label I have provided to Rails, so presumably this Ruby method should be somewhere in my site directory along with other Ruby code generated when invoking "rails generate scaffold..." above (i.e. it wouldn't be in a Rails-specific directory for example). But there is no such method 'edit_post_path' defined anywhere. So is it not really Ruby at all, just something contrived to look that way for some reason, and really just a string of text that is processed by something strictly proprietary to Rails. Is this an example of what is so cool about Rails?
That is a Rails helper method - aka sugar syntax as James pointed out - for routing within your app.
To see all the routes available to you, at the command line do rake routes. You will see a list of the helpers on the left, then you will see the HTTP operation in the second column, third column = URL path format, and last column is a breakdown of the controller and action that it relates to.
To see some of the Ruby code that is at the heart of this magic, check it out in the Rails 3 repo, like this: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb#L444
Also if you want to create custom URLs for specific resources, check out: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/url_for.rb
Here is some more info on routing in general: http://guides.rubyonrails.org/routing.html
Hope that helps.
It's "sugar"-syntax built into Rails. There are a ton of easy methods like this to speed up development.