Can rails handle creating a view without a controller? For example, let say I have a page that just links to other pages, would I need to create a dummy controller for that, or could I just do something in my routes file?
I like August's answer but I have a slightly different method.
Let's say you want to add
/any/path/somefile.html.erb
but not add a controller...
You can just add folder to views called "application", create your file in that directory..
Then in your routes file just add
match '/any/path/somefile' => 'application#somefile'
Your erb still evaluates, you get your layout, and you can create any path you want...
(all this does is remove the need for the pages controller)
Hope it helps...
No. All requests has to go through a controller.
I like to have a PagesController, with map.page ":action", :controller => "pages". That way, I can create app/views/pages/foo.erb and have it available on /foo without any extra code.
Another option would be adding a static html file in your /public directory if you truly don't need it as part of your application.
If you are a brave soul. You can try edge rails 3. Katz demonstrated this possibility on his blog. Here is the link:
http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/
No. All requests have to go through a controller.
If you have a page like index.html.erb and contact.html.erb in your view folder. You need to create a dummy controller called contact. Then you can link to the contact.html.erb from the index.html.erb. And give the link as <%= link_to 'contact', :controller => "ads", :action => "contact" %> here "ads"->controller name.
Related
I have a design.html.erb file in folder views/services/
How do I configure route to this page in routes.rb?
(I have a controller for static pages called static_pages_controller but I don't need any backend actions)
I need something like this:
get '/services/design' => 'static_pages#what_to_write_here'
get '/services/design' => 'static_pages#design'
This would render the design.html.erb inside views/static_pages/ directory.
In order to specify another view in another sub-folder you have to specify in your action:
def design
render 'services/design'
end
This would render the design.html.erb inside views/services/ directory.
get '/services/design', to: 'static_pages#design'
of course if you have method design in your static_pages_controller.rb
I would suggest to use high_voltage gem.
HI i am new to rails i started working on one existing project. i want to integrate new page to website. I created new page under folder Sites(page name xxxx.html.erb) which have controller named sites.and created method within controller
def xxxx
end
when i tried access that page following error is showing
The action 'show' could not be found for SitesController
i think it is problem with routes.rb file please help me
i tried to add this
match "xxxx" => "Sites#xxxx" but not working .......
You need to create a controller named it as SitesController, in this controller you can add the xxxx action.
2 . Then you need add routes in config/routes.rb file. For your reference http://guides.rubyonrails.org/routing.html
Sorry all i got answer problem with i used capital "S" in Sites routes file and used capital "s" in link also <%= link_to "Profile", :controller => "**Sites**", :action => "xxxxx"%>
I have a page app/views/new/news.html.erb and I simply want to link to this page from within my layouts/application.html.erb. Can someone please help! I have been struggling with this all morning.
I have tried things like <%= link_to "News", ... But I'm not really sure where to go from there.
You don't "link" to a view, you link to a controller which renders that view. Then, you'll need an entry in your routes.rb to wire up the url routing for that controller. If you have a controller named NewsController with a method called index and an entry in your routes.rb that looks like resources :news the following link_to should work: link_to "News", news_path.
In case it's not clear, the index method in your NewsController needs to have render :news in it.
Sounds like you may want to check out the guide on this topic: http://guides.rubyonrails.org/routing.html
If you have it setup correctly you should have a plural for your controller (i.e. news instead of new) and the following should work:
<%= link_to 'News', :controller => "news", :action => :news %>
This is assuming you are using scaffold.
If are adding this folder and page manually: for dynamic page, you have to create an action in your controller. For static page, you have to put it in your public folder.
You can always run
rake routes > routes.txt
in your application directory, which will dump a list of all routes into a txt file. Choose path that leads to action and controller you want, and then supply it as a param for link_to method :)
An identical question has been asked before
AssetTagHelper::image_path outside views
but the solution does not work for Rails 3.
I have a requirement where I am returning some data using JSON format and part of the data needs to return the full path of the image. I am building the response in the controller and calling render :json. I am also specifying an asset_host in the environment.rb so I need a way to include that in the returned data from inside a Rails controller.
You can use view_context in your controller when doing 'view' tasks like generating links. The good thing about it is you don't have to include the view helpers in your controller.
e.g. in your controller you can create a variable which will be a html link with link_to or url_for if you just want the link, 'only_path' option set to false should give you absolute url.
link = view_context.url_for(:action => 'login', :controller => 'members', :only_path => false)
Hope this helps.
I'm trying to build a CMS in Rails from scratch, and for showing the user generated pages I'm having trouble deciding exactly how to do it.
The way I have it right now, I have a controller named 'content' with a single action called 'show'. In routes.rb I have a rule that passes any name after the name of the website to the content controller, show action with parameter name.
For example, www.mysite.com/about_us would route to
:controller => 'content', :action => 'show', :page => 'about_us'
Inside the content controller, I do a find on the Pages model to locate the named page:
#markup = Page.find_by_name(params[:page])
And then in the show.html.erb view I use the raw helper to display the content:
<%= raw #markup.text %>
Does this method violate anything about the way I should do be doing things in Rails? Or is this an OK solution?
I ended up using the sanitize helper, by default it removes <script> tags which is essentially what you need to prevent XSS, as far as I understand. For those who have found this via a search, the only code that changes from what I described above is that in the view you change to:
<%= sanitize #markup.text %>