actionview

How do I implement Section-specific navigation in Ruby on Rails?

I have a Ruby/Rails app that has two or three main "sections". When a user visits that section, I wish to display some sub-navigation. All three sections use the same layout, so I can't "hard code" the navigation into the layout. I can think of a few different methods to do this. I guess in order to help people vote I'll put them as ans...

How do I access "assigns" after "render :template => ..."?

I have an error handling method in my ApplicationController: rescue_from ActiveRecord::RecordNotFound, :with => :not_found def not_found(exception) @exception = exception render :template => '/errors/not_found', :status => 404 end In RAILS_ROOT/app/views/errors/not_found.html.erb, I have this: <h1>Error 404: Not Found</h1> <%= d...

How can I test views in a Rails plugin?

I'm writing a Rails plugin that includes some partials. I'd like to test the partials, but I'm having a hard time setting up a test that will render them. There's no associated controller, so I'm just faking one: require 'action_controller' require 'active_support' require 'action_pack' require 'action_view' class MyTest < Test::Unit...

How do I render a partial of a different format in Rails?

I'm trying to generate a JSON response that includes some HTML. Thus, I have /app/views/foo/bar.json.erb: { someKey: 'some value', someHTML: "<%= h render(:partial => '/foo/baz') -%>" } I want it to render /app/views/foo/_baz.html.erb, but it will only render /app/views/foo/_baz.json.erb. Passing :format => 'html' doesn't help. ...

Rails: Using form (collection select) to call show-action.

A model named 'book' with attributes 'name' and 'id' is given. How can i use this collection select to call the show-action of a certain book? The one code mentioned below returns the following error message: Couldn't find Book with ID=book_id <% form_tag(book_path(:book_id)), :method => :get do %> <p> <%= label(:book, :id, 'Show ...

Nested Object w/ Checkboxes - mass-assignment even with accepts_nested_attributes_for ?

I thought that there should have been a simple solution to this, given that Rails 2.3 has this newfangled nested forms feature. Basically I want to create or update a user and assign them roles at the same time. It seems like I'm doing everything right but I get the error WARNING: Can't mass-assign these protected attributes: roles_attr...

Rails validation and 'fieldWithErrors' wrapping select tags

Is it normal behaviour to not get the <div class="fieldWithErrors"> wrapped arround select tags that have validation errors? I personally see no reason why the select tags should be treated differently than other form tags (input, textarea). I do get the error in error_messages_for and error_message_on methods for that field. PS. I hav...

How do I expire a view cached fragment from console?

Something like Rails.cache.delete('site_search_form') doesn't seem to work. Is this possible? Thanks. ...

render default template when requested template is missing in Rails

For a plugin I want to hack the following feature into Rails: When a (partial) template does not exist (regardless of the format) I want to render a default template. So say I call an action 'users/index' if users/index.html.erb does not (or other format) exist, 'default/index.html.erb' should be rendered. Similarly, If I call an act...

Rails: Preselect a value in ActionView-Helper 'collection_select'

I'm trying to get the ActionView-Helper collection_select to take a value that will be preselected in the dropdown-menu. Neither (:selected in the html-option-hash) <%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %> nor (:selected in the option-hash)...

Where does Rails use REXML lib and how to require that part?

I have a gem that uses some of the Rails libs, including action_view and active_controller. Though I require those libs, all tests fail when trying to call some has_text? method, which (as I found out) is part of'rexml/element' lib. Here's what I get: undefined method `has_text?' for "<b>Hello,<i>world</b></i>":String Requiring 'rexml...

Rails form_tag form writing - with non-active record model

Hey Rails People, I'm somewhat of a Rails newbie. I'm writing a couchrest-rails app, so am not using activerecord for this model. I just figured out that that means that form_for(@model) won't work. I'm trying to work out how to use form_tag -- but most of the examples don't involve new & create actions. This is wrong: <h1>New ...

Rails XML builder with no pretty-printing (i.e. minified XML)

I am using Builder::XmlMarkup to produce data structures in XML format for a RESTful API server. Recently, I discovered a bug where the pretty-printing from Builder::XmlMarkup produced an element full of whitespace text instead of an empty element as it should. For example, this code: xml.outertag do xml.list do # Some code whic...

One controller, different views for normal users and admins

Hello everyone, in my application, I have a "User" model. Each user can have multiple (email) addresses which are defined in the model "Address": Class User < ActiveRecord::Base has_many :addresses def is_authorized(op) # returns true or false end def is_owned_by(user) # returns true or false end end Class Addre...

Form Action garbled, Rails 2.3.3

I seem to have a problem that I can't find the solution for myself, I hope someone can help. I have a form defined like so: <% form_for @leads do |f| %> I have a resource called @leads (map.resource :leads) But when I look in the HTML code of the page it generates, I see as a form action the following <form action="/lead.%23%3Clea...

How can I stop the password field being pre-populated on edit?

I have this problem all the time in my rails apps and I still need the correct solution. Whenever a user edits their own record the password field is being populated. I suspect its Firefox as setting @user.password = nil in the edit action doesn't help. The problem is the password confirmation isn't populated so validation fails due to ...

Rails form with multiple nested models causes issues with radio groups

I'm having a problem with nested model forms that contain radio buttons, when I have multiple models all the radio buttons are treated as being in the same group. My model contains a has_many relationship like this: class Order < ActiveRecord::Base has_many :order_items accepts_nested_attributes_for :order_items end Class Orde...

How do i create a form that inputs multiple items of a model?

I have a simple point of sale application written in ruby and rails, and hobo. Originally intended to be for only one product at the time, now the client wants to add multiple products into the sale model Besides that i am using brands for categorizing products and in my new sale form i use ajax in order to populate a select product me...

Load ruby on rails template from database

I wonder if there is a way for me to store ruby on rails view files into database store and have that fetched directly from there. The reason is I want to create a CMS with all the user data stored in database, so I would like to have the template stored in database but still retain the whole ActionView mechanism. ...

rails/ruby - how to rescue ActionView::TemplateError

i am testing an application built with rails and from time to time get bugs when it happens, ActionView::TemplateError occurs and gets logged in log/production.log how do i catch it and f.ex. send myself by email whenever it happens somewhere in the app? what is the best way to do that? f.ex. in ApplicationController with some sort o...