Possible to use Rails with no HTML? (Just Ruby) [closed] - ruby-on-rails

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a Rails beginner messing around with a super simple app.
What I'm wondering is whether you can use Rails strictly with the Ruby language and forget HTML all together.
Why I'm asking:
In my /testapp/app/views/welcome/index.html.erb file, (which is my homepage), I tested it with the code:
puts "Hello World!"
When I navigate to the page using the rails server the text includes the puts. I even changed the file name to /testapp/app/views/welcome/index.rb (omitting the html), it didn't work.
Can I use Ruby like this? Or do I need to modify something within Rails to use Ruby like this? I don't get it.
Thanks!

If you are going to be learning Rails you will need to do it in conjunction with HTML. When I learned I started with ERB and then moved to Haml.

If you want the page to just show static text you can put Hello World! right in the index.html.erb file.
Mostly though, you'll use these views to dynamically create web pages for you. That way if your website is storing books and users continue to add more books to the website, your index page will automatically include all the books everyone entered. In your index.html.erb that code would look something like:
<tbody>
<% #books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.body %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', (edit_book_path(book)+"/hello?") %></td>
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
I highly suggest running in your console:
rails generate scaffold books title:string author:string
Run the migration:
rake db:migrate
Fire up your server and go to localhost:3000/books
This should give you a visual example of what I'm talking about and then you can walk through the code to help you understand what Rails is doing.

Related

Can someone explain to me what this code is doing?

I am currently building an online course and I was having trouble accessing a lesson that is associated with a certain course.
A developer friend of mine solved the problem for me but I'm not really sure why this code works and if there are different ways, more of a Rails conventional way to write this code.
<% #courses.each do |course| %>
<tr>
<td><%= link_to course.title, "courses/#{course.id}" %></td>
</tr>
<% end %>
I am not sure what this part "courses/#{course.id}" is doing. Is there a way to write this using a more conventional seeming names route helper?
It should be the same as course_path(course)
This call just figure out the path for you. The expression in your code simply build this path putting together "courses/" and the id of the course (but using interpolation, not concatenation).
As Ursus answer explains, courses/#{course.id} creates URL directing to specific course path by using string interpolation. For example, if #courses variable is an array with Course objects with ids: [1, 2, 3], then you will receive links directing to "course/1", "course/2", course/3".
To replace that interpolation, you can simply write
<%= link_to course.title, course %>
It will create the same output as "courses/#{course.id}"
To learn more about string intepolation, you can start here: http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html
For some reason your friend did this:
<td><%= link_to course.title, "courses/#{course.id}" %></td>
...instead of this:
<td><%= link_to course.title, course %></td>
...and I have no idea why. The second example is how you use links in Rails. The first example doesn't safeguard you against possible future URL changes.

Generating proper paths in namespaced Rails scaffolds

When you use rails generate scaffold admin/user --model-name=User or rails generate scaffold_controller --model-name=User it generates almost everything in a namespaced fashion. You get app/controllers/admin/users_controller.rb with your controller and app/views/admin/users/ filled with your views.
The one thing it doesn't get right is your paths. You have to manually go and replace references to user_path with admin_user_path and the like. This is pretty tedious.
Is there a way to tell Rails to generate the paths to point to your new namespace, rather than the namespace that the model is in?
Using Rails 4.
With rails build-in generators you can't.
See the generator source code to understand why:
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
<td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
As you can see, it goes with edit_<%= singular_table_name %>_path to generate the edit path, without considering name-spacing. (And haml-rails does the same)
The best thing to do, if you have time and patience for it, would be to fix this on the codebase and propose a PR. That's the main point of open-source after all.
If you go this direction, have a look first at open issues, I haven't dive deep into but it seems that different conversations are going on about that matter. Like https://github.com/rails/rails/pull/13927 or https://github.com/rails/rails/issues/21652
Or you can use existing gems like Beautiful-Scaffold that seem to be supporting namepacing

Weird rails pluralization issue

I have a ruby on rails application that recently started giving me issues.
I believe there may be a weird bug/feature in the way rails is pluralizing model names for the database.
For example,
I have a model called DiagExerciceWeekFive. The table in the database is called diag_exercice_week_fives. The pluralization works correctly here.
I think there may be a problem in the way rails is attempting to "de-pluralize" the table into the respective objects.
When I try to load up a simple form that displays all of my diagweekfives, I get this error:
uninitialized constant Diag::DiagExerciceWeekFife
Not once have I used that name in my application.
Here's the relevant bit of code that is throwing the error:
<% ExerciceWeekFive.all.each do |exercice| %>
<tr class="success">
<td><%= check_box_tag :exercices_week_five_ids, exercice.id, #diag.exercices_week_fives.include?(exercice), :name => 'diag[exercices_week_five_ids][]' %></td>
<td><%= exercice.number %></td>
<td><%= exercice.description %></td>
</tr>
The exception is thrown on the first <td> within the <tr>
Has anyone run into this before? I know little about rails, but I am trying to maintain some legacy code.
Thanks.

model and controller different namespace , achieve scaffolding

Need some tips on how to achieve.
Normally we would do this for scaffold:
rails g scaffold User email:string password_hash:string password_salt:string (and exit)
However, due to the complexity of what I want to achieve in mind, thus the following:
Admin users, where "Admin" is the namespace
rails g scaffold Admin::User email:string password_hash:string password_salt:string (and etc)
Items, where "Application" is the namespace. Items is a model that will be access by different namespaces.
rails g scaffold Application::Item name:string cost:float quantity:integer
So, obviously "Admin" namespace is used for Administrators to login and they could manage items.
However, I'm creating the rails app in such that I has multiple login for different application such as
Namespaces(again)
- Application1
- Application2
So you would expect the main page of each Application to be below as respectively:
http://my.railsapp.com/application1/index
http://my.railsapp.com/application2/index
However, I want to achieve scaffolding, thus Application::Item can be used in any namespace like Application1::Item.
Example would be in my controller(application_a/items_controller)
#items = Application1::Item.all
And in my views, like rails scaffolding
...
<% #items.each do |item| %>
<tr>
<td><%= item.email %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_application1_item_path(item) %></td>
<td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
...
To explain my reasons. I would like Admin to create, and edit any field for Items.
Application1 to edit the cost or quantity
and finally Application2 to update only the quantity.
Ofcourse, its more to just restrictions, but to put it simply is different application utilising on the same data model.
Thanks.
Scaffolding is for most common cases. If it can't meet your need on customization, then don't use it, create models and controllers separately. No need to waste time on digging that.

Why does rails split my model named menuitem into menu_item when i run generate/scaffold? Is it breaking my app?

When I generated a scaffold for a model class named menuitem, it generates a class file for menuitem, but in the database creates menu_item and refers to the class in the views and controller as menu_item. This may be causing me quite a headache as im genereating a newsted show link, but its failing telling me missing method. The rake routes tells me the show route should be menu_menu_item, but when i do:
<td><%= link_to 'Show', menu_menu_item(#menu) %></td>
it doesnt work.
Is that because of the funky two word class name?
You must have generated either menu_item or menuItem or MenuItem. It doesn't know where one word stops and another starts unless you tell it.
Also, for your link_tos, you just need to append _path:
<td><%= link_to 'Show', menu_menu_item_path(#menu) %></td>
Well, actually, that looks a little wrong to me. That looks like you're trying to go to a single item, which I think will require you to specify both the menu and the item:
<td><%= link_to 'Show', menu_menu_item_path(#menu, #menu_item) %></td>
And to all the menu items in a menu:
<td><%= link_to 'Show', menu_menu_items_path(#menu) %></td>
The Pluralizer shows you show things should be named according to the Rails' conventions.

Resources