When I run my initial migration I get a "new.html.erb" file for each of my models. Its a very simple looking form, but the "new.html.erb" contains just the following code..
<h1>New city</h1>
<%= render 'form' %>
<%= link_to 'Back', cities_path %>
Now I want to modify this page, but I dont really understand whats going on here, where in the project this 'form' is located?
The form is located under app/views/cities/_form.html.erb.
I strongly suggest you to read this page before start working with ruby - it will give you a better understanding of how things works.
Since Rais works on convention over configuration, you will have a bad time trying to figure out everything by yourself. However, once you are used to the conventions, your will have a very fast development speed.
In the same folder where your new view is located.
Related
Is there a way to open .html.erb files by themselves in the browser. The Rails app I am trying to have a look at is in rails 2.3 and will not run locally on the rails server nor will it run when I give the command script/server.
I would just like to open the individual views in the browser by themselves...is this possible?
You can open them in the browser, but the <% %> and <%= %> tags will get shown as-is instead of executing the Ruby code. These are template files that need to get processed by Rails to fill in data at runtime. Your best bet is TsaiKoga's answer, making a controller action render the template in question.
In rails4 you can do it like this:
erb a.html.erb > a.html
Maybe it doesn't work in rails 2.3. If you want to render that view, maybe you can use an action just like users#index and change the code to render it:
render "devise/mailer/a.html.erb", :layout => false
Please help... :) Working on this for a couple of days now
I have a submittal database with stored url's to manufacturer installation instructions generally in PDF formats. I don't want to use the default to_path because I'll have to store the documents locally on my server. Here's the snippet of code that I'm using to pull the url path from my database. As you can see below without the path_to rails wants to turn the url into a path (see error below). I tried adding quotes but then rails doesn't read the code correctly. I've read about helpers but haven't gotten them to work.
<%= #wf_room.wf_lights.pluck(:typemark).count %> <%= link_to #wf_room.wf_lights.map(&:typemark).uniq, #wf_room.wf_lights.map(&:url).uniq %>
error:
undefined method `http://www.sistemalux.com/en/files/ficheproduit/7050_Sliver_wall(13).pdf_path' for #<#:0x0000000404bad8>
Have you tried string interpolation?
<%= link_to #wf_room.wf_lights.map(&:typemark).uniq.first, "#{#wf_room.wf_lights.map(&:url).uniq.first}" %>
(I added first method because won't they the current methods return arrays?)
The release notes for Rails 3.0 include this change:
7.4.2 Helpers with Blocks
Helpers like form_for or div_for that insert content from a block use <%= now:
<%= form_for #post do |f| %>
...
<% end %>
Your own helpers of that kind are expected to return a string, rather than appending to the output buffer by hand.
Helpers that do something else, like cache or content_for, are not affected by this change, they need <% as before.
We're in the process of migrating a web application from Rails 2.3.18 to Rails 3.1.12, and it would be very useful to have a complete list of such helpers that have changed, so that we can check all of their occurrences in our source code, but I'm having trouble finding an authoritative list of this kind.
I've tried looking through the git history of the rails project, but there seem to be many commits with related changes, and they're not obviously grouped on particular branch. For example, it seems to be clear that this list includes:
form_for
form_tag
fields_for
field_set_tag
... from 7b622786f,
link_to
... alluded to in e98474096 and:
div_for
content_tag_for
... alluded to in e8d2f48cff
remote_form_for
.... alluded to in 0982db91f, although it's removed in Rails 3.
However, I'm sure that's not complete - can anyone supply a complete list?
i don't have a complete list, but i think that you can derive most of what has changed from having a look at the diff in documentation of UrlHelper and FormHelper. most of the methods in those helpers changed to the new syntax.
http://apidock.com/rails/v2.3.8/ActionView/Helpers/UrlHelper/link_to
http://apidock.com/rails/v2.3.8/ActionView/Helpers/FormHelper/form_for
There is a list of these methods in the rails_upgrade plugin, whose purpose is to check your application for problems on upgrading from Rails 2 to Rails 3. The relevant method is check_old_helpers, which checks for block helpers containing any of:
content_tag
javascript_tag
form_for
form_tag
fields_for
field_set_tag
As for how authoritative this is, this plugin is an official Rails project plugin, although it does miss out a couple that I found by searching the git history:
div_for
remote_form_for
link_to
However, if the official tool for checking for these helpers is missing some, perhaps this is as good a list as I'm likely to find. Another point is that the upgrade check tool mentions that there should be deprecation warnings if you miss some, which provides an additional check:
Block helpers that use concat (e.g., form_for) should use <%= instead of <%. The current form will continue to work for now, but you will get deprecation warnings since
this form will go away in the future.
I have a Fixer model, but, unlike all the other models in my app, its routing isn't working right, even though there's a resource line for it in the routes file.
The problem is, when I try to link to the basic show path any number of ways, like
<%= link_to "Fixer", fixer_path(#fixer) %>
or
<%= link_to "Fixer", #fixer %>
or
<%= link_to "Fixer", fixer_path(#fixer.id) %> # I got desperate
it links to /fixers.[:id] (not a real page) instead of /fixers/[:id]. No idea what's happening, cause my resources line is there, and show is a basic resources action, and all the other similarly resourced models seem to be working just fine.
Any ideas? (I can put more code up if necessary. Just not sure what would be relevant).
EDIT -- The Fixers output in my rake routes:
fixers GET /fixers(.:format) fixers#index
POST /fixers(.:format) fixers#create
new_fixer GET /fixers/new(.:format) fixers#new
edit_fixer GET /fixers/:id/edit(.:format) fixers#edit
GET /fixers/:id(.:format) fixers#show
PUT /fixers/:id(.:format) fixers#update
DELETE /fixers/:id(.:format) fixers#destroy
Whoa. Just noticed after posting this that the 5th line is missing the "fixer" before the show action line that all my other models have. Why would that have happened? How do I fix it?
EDIT -- I figured it out! Really dumb issue. For some reason, back when I was learning how to do all this, I both included a resources line and added this line above it:
match '/fixer', to: 'fixers#new'
When I took that line out (cause it was redundant), the problem went away. I guess I was messing with the Rails routing automagic. They really do make those defaults the best option.
Hey I have had this same problem before.. and unfortunately have resorted to doing the following: link_to "link text", "/fixers/#{#fixer.id}" I would love to know the actual correct answer for this though.
I am trying to learn ruby and learn how to handle a while request round trip.
On my index.html.erb page I added this line:
<%= link_to "Alex Link", test_path(#test) %>
but I got an error:
undefined method `test_path' for #<#<Class:0x4064e80>:0x3c0b5c8>
As I understand it, I need to add a record to routes.rb, and then a controller. Correct? How do I do that?
I read the explanation for this in the Rails Guides, but just finding it a bit confusing doing it the first time.
For your purposes (learning) resources tests if fine.
It also gives you other routes for free, see RESTful routes.