Rails default link when using hash sign - html

I'm working on a Rails project on the file located at myserver/myNewProject/here.html, and when I have an HTML link like
...
it jumps back to my old project at myserver/myOldProject#test. This is not good, since the old project has nothing to do with the new project.
This is probably because I copied over some code from my old project, but I don't know which part is causing this. How can I correct this behavior?

Use the :anchor attribute in the link_to:
<%= link_to('some link', posts_path(:anchor => 'test'))
Will generate a link like:
some link
See the docs for all the options link_to takes:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

Related

how to add form to application.html.erb page ruby

I'm quite new to ruby and I'm trying to put the form to add a new post to the "application.html.erb" page.
Somehow I can't get it to work by just copying the code from the "app/views/things/_form.html.erb" page, and also I can't seem to get it to work by copying the code from the "app/views/things/new.html.erb" page.
If I do that I get the error:
First argument in form cannot contain nil or be empty
The error points towards:
<%= form_for(#thing) do |f| %>
This is a code that I find in the "app/views/things/_form.html.erb" page.
Can anybody point me in the right direction? To be honest, I don't even know where to start looking for the answer.
I think you should take a look at ruby on rails MVC model first, and also go through a basic tutorial. Just a quick review of you question:
application.html.erb is usually in the layout folder of rails,
it's job is to provide a root template/layout for the whole project if not specified.
usually you don't want to put any code that is not commonly
needed by all pages.
the reason it is giving you the error First argument in form cannot contain nil or be empty is because, the helper method form_for is expecting the controller to provide a #thing to the view file application.html.erb, in your case, it is obvious that #thing is not exist in your controller or your ApplicationController.
The #thing should be initialized in your controller. I suppose you've got a things_controller.rb that has a new action in it. In this action, controller should initialize #thing and therefore it is visible in app/views/things/new.html.erb.
You really shouldn't put forms in application.html.erb, but just to make the error disappear, you could do
<%= form_for(Thing.new) do |f| %>
(assuming you've got a Thing model in app/models/thing.rb).
This way you'll initialize an instance of Thing right in your application.html.erb view.

Creating dynamic links for a HTML email with embedded ruby

I have a simple question about links in HTML emails
I am trying to add a simple dynamic link in an HTML email to my site like this
MySite
This fails, but this works
MySite
I am assuming its to do with the embedded ruby but strangely, the links work if I view them on my iphone.
Any thoughts on this?
Stupid me....
I needed to construct the link using the ActionMailer generating URLs instructions, see here http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
My URL is
<%= link_to "View response", notification_micropost_response_url(#micropost, #response) %>
The default host must be first set in the dev and prod environment config files:
Example (development.rb)
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Link to home page renders blank

In my rails app, I have a link to the root that renders in all browsers as
<html>
<head>
<style type="text/css"></style>
</head>
<body>19</body>
</html>
Server-side I can see all assets getting passed back. For some reason the browser will not display them because if I view page source I can see ALL my markup correctly.
Now if I load or reload the page localhost:3000 normally the page and all assets are displayed correctly. If I simply refresh I get nothing.
The only time this occurs is when I click a link back to the homepage like a href="/".
The behavior does not occur if I hardcode a href="localhost:3000/" but I don't want to do this.
Rails app, Turbolinks, nowhere in code do I use the number 19, rake routes with root GET well-defined, no public/index.html. I suspect something to the degree of caching or turbolinks but I have no clue how to resolve.
Edit: Config/Routes.rb
SCRR::Application.routes.draw do
root 'home#index'
#pages
get 'about' => 'pages#about'
get 'history' => 'pages#history'
get 'links' => 'pages#links'
get 'safety' => 'pages#safety'
get 'membership' => 'pages#membership'
get 'events' => 'events#index'
get 'grand_prix' => 'grand_prix#index'
get 'newsletters' => 'newsletters#index'
end
Edit 2: Chrome Devtools Console Errors
This error is probably more relevant
Resource interpreted as Font but transferred with MIME type application/x-woff: "https://netdna.bootstrapcdn.com/font-awesome/2.0/font//fontawesome-webfont.woff".
jquery-2.1.0.js?body=1:1078
25115 : CS -> BG : FAILED closepopuptoplevel
onloadwff.js:77
From what you've posted, I don't know whether this would be a Rails issue or a browser issue.
I see you've tagged turbolinks, which could be a contributor to the issue; so let's have a look as to what the problem might be for you:
ERB
The ERB code you should use should be the following:
<%= link_to "Home", root_path %>
If you're trying to reference the "home" url directly, there may be an issue with how you're rendering the URL, hence why it won't show. You should ensure you use the path helper as described above.
This should be coupled with the correct routes:
#config/routes.rb
root "home#index"
resources :pages, path: "" do
collection do
get :about
get :history
get :links
get :safety
get :membership
get :events
get :grand_prix
get :newsletters
end
end
Assets
You mention the assets don't render when you refresh the page, this could be an issue with Turbolinks, although I'm not sure
Turbolinks basically just reloads the <body> of the page, leaving the <head> intact. Although this might sound like nothing to do with your issue, perhaps the problem is to do with the way in which Turbolinks is working with your assets
From your edit, it may appear that your assets are not loading correctly, or are at least not being shown correctly.
I hope this helps, I doubt it will give you a constructive answer

How to link to another view (from same controller) in rails from html erb file?

So I have my main page, landingpage.html.erb, and I want to put a link on there that will lead to another simple page with a few more links to outside sources.
landingpage.html.erb
<%= link_to "press", '/press', :method => :get %>
press.html.erb
<h1>Test!</h1>
(I also made a partial, to test if that would get displayed. Its the same is press.html.erb but _press.html.erb)
Controller
def press
end
Error I keep getting:
Routing Error
No route matches [GET] "/press"
Where am I going wrong? Thanks.
You need to add the route to your config/routes.rb too. It depends on how you defined your other routes but a basic definition look like this:
get '/press', to: 'controller_name#press'
Read more about Routing in the Rails Guides.

CSS only shows correctly on index page. Ruby On Rails

I am new to Ruby On Rails and am trying to get a css template to work for a project. It works fine on my index page but on every other page it doesn't seem to be getting the complete layout from the application.html.haml file.
If I use the link to include the css in the page it will only add font changes and color changes, but none of the actual layout changes ie. tables, formatting and such.
= stylesheet_link_tag 'style'
I'm not to sure where to troubleshoot now since it's only grabbing half my styles from the style sheet.
!!! 5
%html
%head
%title HELPDESK
= csrf_meta_tags
%link{:href => "style.css", :rel => "stylesheet", :type => "text/css"}/
%body
%h1.title HELPDESK
#main
- if flash[:notice]
#notice.message= flash[:notice]
- elsif flash[:warning]
#warning.message= flash[:warning]
= yield
If it's any help, I can rename my other pages to index.html.haml and the css loads perfectly fine, so the pages are written correctly I just have an error with the way Rails loads stylesheets or an error with a route.
tl:dr everything in my Rails app after site.com/questions/ does not load with the correct stylesheet, while site.com/questions loads perfectly fine.
Thanks!
Alright, for anyone that reads this later, I fixed the problem. I needed to make the link in the application.html.haml file (listed above) point to ../style.css instead of style.css
I fixed the problem. I needed to make the link in the application.html.haml file (listed above) point to ../style.css instead of style.css