How do I get access to params in my Rails class - ruby-on-rails

I have a class, called Tab, that needs access to params so that it can check params[:controller]. Two questions? (1) Where should I put these two classes, Tab and Tabs? Should they be in lib? Or in application_helper.rb? (2) What's an easy way to get access to params in that class? Should I just add a params parameter to the constructor of Tabs and pass params in at the call site?

1) Where should I put these two classes, Tab and Tabs? Should they be in lib? Or in application_helper.rb?
it can go to lib, ideally we keep all custom classes/modules in lib directory.
2) What's an easy way to get access to params in that class?
I think you are right here, you should add a params parameter to the constructor of class and pass params in it.

Related

How to list all Rails 3 controllers and its public methods

I would like to create a checking tool/rake task, that would dynamically go through all the public methods that are possibly accessible from outside world and check our authorization rules.
For this I would need to collect all the controllers and all its public methods. How to do that? I would like to use Ruby reflection or metadata techniques rather than grepping files.
AFAICT this is impossible to do in the general case as you can't tell which params go where, e.g. /items/foo/id or /items/foo?bar=baz ? Also, which values of the params are ok ?
But you can get a decent value with
Rails.application.routes.routes.map(&:path)
which shall give you a list in the form used in rake routes (/klass/:id/action(.:format))

MVC Razor How to get the model in the Controller on HttpPost when the model is dynamic

I'm working a feature in the application where model will be dynamic in the sense that any settings data could be displayed and the view will get the model based on what tab they clicked on. I use Hidden field to store what the settings name was because they are same as model name. for ex., if tab1-> Settings1 then Model is Settings1[already exists in the Model].So I used # model dynamic in View and used #Html.EditotForModel() to draw the required UI based off the model. My problem is when I do HttpPost on Edit currently I'm using FormCollection to read the data on that page when I declare the model name in the param it will get it for me but I don't know which model is coming back other than by the Hidden variable and I need it because the Model validation is broken because of this issue. Any help or feedback is appreciated? I can give more details if required? Has anybody crossed this issue before??
Dynamics can be a good thing and a bad thing. Using them on models that have a common interface in a controlled manor is best.
There are different options that you can look at:
1)
Have you tried making the action method accept a dynamic type? That might be the easiest way.
You might have to set up a casting helper to cast the object to the correct type based on the hidden field.
2)
I have a similar idea in some code, but I created a viewmetamodel class that contained all my types as nullable properties. My action method accepts this viewmetamodel type and validates the properties that are not null.
In line with this, if your data is not too large, then you could load all the settings tabs and use Jquery apply the tab with on click.
3)
You could also create #sections or use EditorFor(c=>c.settings) for each tab. That way each tab will load a type safe object. You would need to create controllers for each.
I would say pick the easiest method for you. I hope that this at least gives you some ideas.

How to reference a grails GSP model variable indirectly e.g. via .get(...)

I'm using a GSP for sending out emails based on the MailService plug-in. The sendMail closure passes (amongst others) body(view:..., model:myModel)
I know that I can access every item of the myModel Map just using ${itemName} in the GSP. However as I sometimes want to build the item name dynamically like 'item'+i, I need to have some surrounding method to access the variable.
I already tried ${model.get('item'+i), and ${params.get('item'+i), but model is null and params is an empty Map. I also tried pageScope, but though I can access an item via ${pageScope.itemName, I can not use ${pageScope.get('item'+i)} because pageScope is not a Map.
Probably there are multiple solutions to solve this; I'd be glad about an easy one ;-). One solution I see is to pass myModel as the only parameter and then always use myModel.get(...), however this would mean that I had to change all my existing GSPs to always refer to myModel instead of accessing items (with fixed names) directly; so if there's a way where I don't have to change the model passed to the GSP, this would be my favorite.
If someone could also say a few words about the difference of model and params in this context, this would be additionally helpful!
I've managed it now using ${pageScope.getProperty(...)}.
There's no 'model' scope or variable. Instead objects in the model map are set as Request attributes to make them available to the GSP. This is a Spring feature which makes it easy to access variables in JSPs using JSTL and since the GSP syntax is very similar to JSTL it works the same way in Grails.
So you can use this:
${request.getAttribute('item'+i)}
to access model variables using dynamic names.
You can use ${fieldValue(bean: book, field: 'title')}
See: http://grails.github.io/grails-doc/latest/ref/Tags/fieldValue.html

How do I create the Controller for a Single Table Inheritance in Rails?

I am setting up the Single Table Inheritance, using ContactEvent as the Model that ContactEmail, ContactLetter, and ContactCall will all inherit.
But I'm stumped on how to create the routing and the controller.
For example, let's say I want to create a new ContactEvent with type Email.
I would like a way to do the following:
new_contact_event_path(contact, email)
This would take the instance from Contact model and from Email model.
Inside, I would imagine the contact_event_controller would need to know...
#contact_event.type = (params[:email]) # get the type based on what was passed in?
#contact_event.event_id = (params[:email]) #get the id for the correct class, in this case Email.id
Just not sure how this works....
I had similar problem.
See here how I solved it.
I would have a controller (and maybe views) for each of your resource types. So add a controller for ContactEmail one for ContactLetter etc. Don't bother with one for the base class ContactEvent. Then your paths would read something like:
new_contact_email_path(#contact) or new_contact_letter_path(#contact)
The controller actions would then use the right model for that they represent, ie:
#contact_email = ContactEmail.new(params[...])
If you can keep your three types of resources separate rather than trying to pass the type in and building the right object in one controller you should find life much easier. The downside is you may need more links/forms/views in the front end, however depending on your application that may not be a bad thing from the user's perspective.

How can I programatically access the ASP.NET route table from a filter?

Is the route table available from the filters?
I'm inheriting from the AuthorizeAtrribute class. All I'd like to do is examine the route table, and then just call the base class (after making some minor setup adjustments). I could look at the raw url, but I'd rather get the value I need from the route table. Any ideas?
Figured it out. Turns out you can instantiate an instance of System.Web.Routing.RouteTable by passing in and HttpContext.

Resources