Making an Object (I think object) linkable - Ruby on Rails - ruby-on-rails

Trying to figure out how to make my thing.name a clickable URL that redirects to thing. Seems easy enough, but I cannot figure it out. Maybe I shouldn't be using the link_to to achieve this?
Currently I have the following
<% thing.each do |thing| %>
<tbody>
<tr>
<td><%= thing.name %></td>
<td><%= link_to 'Show', thing %></td
Trying to figure out how to make thing.name a URL, so that I can remove <td><%= link_to 'Show', thing %></td

Typically it would be
link_to thing.name, thing_path(thing)
thing_path here would be a dynamic form of an URL Helper. To work properly, the route to thing in your config/routes.rb file must be properly declared. See the Rails routing guide for more details on that.

i don't really get what you wanna achieve but do you mean you want to make something like url www.blabla/thing/name", if you wanna do it you would like to use gem friendly_id

Related

Ruby on Rails - Unable to Display Associated Item From Database (#<Comment:0x44f9ec8>)

I followed the "Getting Started" tutorial at http://guides.rubyonrails.org/getting_started.html, and wanted to change what is output on the articles index page.
rather than showing the text of the article in the main table, I want to see the most recent comment made on that article.
Here is the code for the index:
<tr>
<td><%= article.title %></td>
<td><%= article.comments.select(:body).last %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
Where 'article.comments.select(:body).last' is meant to show the body of the latest comment made on the article in question (this table iterates for every article).
Instead of seeing the text from the comment however, I see this:
{Comment:0x44f9ec8} (with sharp brackets instead of curly - wont let me post the sharp ones.... <>)
I also tried editing comment.create in the comments_controller with:
#article.text = #comment.body
After setting the index table field to 'article.text' (which works fine), but with no effect.
Anyone have any idea what I'm doing wrong??
Any help is much appreciated.
article.comments.select(:body).last does not do what you expect
the select part uses this select which alters the SQL command you use. It forces selecting only the body field from comments and returns a comment object.
so you need to change your code to article.comments.select(:body).last.body
If you need to have all the body fields and just use one, I suggest that you use pluck if you are on rails > 3.2
article.comments.pluck(:body).last

How does linking in an html.erb document work?

<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

Partial vs. Helper - Difference, but most, performance today

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.

What is edit_post_path from a Ruby standpoint

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.

Need help with rails' basics

Hello I want to make simple shopping cart. I am a beginner in programming. So please help. I have items with prices and quantities. I want to place button "Plus One", so when i press it should add one to quantity.
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_item_path(item) %></td>
<td><%= link_to 'Destroy', item, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%= link_to "Plusone" %></td>
Innocent suggestion. Buy a copy of Agile Web Development with Rails 4th Ed If I remember correctly this is the same example solved there through out the book. And it worth buying.
To be frank, this is kind of a difficult problem for a beginner. If you want a button that does nothing but add one to the quantity, you're talking about defining a custom action, so you need to read up on Rails routes. Here's a good Railscast addressing this. There's also a follow-up episode.
Next, if you want this to dynamically update a field on the page without reloading, you're talking about an AJAX call to your custom action. Here's a Railscast for Rails 2, and here's a Railscast for Rails 3. These should have the information you need to make an AJAX call to your controller action and handle the response, either with a success handler or a js.erb template.
In addition to the sources mentioned above, other useful sources are: Ruby on rails guides, 'Head first rails' book:)

Resources