Creating a Rails partial that is model-agnostic - ruby-on-rails-3

I have the two Ruby on Rails models SafetyTest and TeamDue, and want to create a partial called _seasonal.html.erb that can work with either models.
The real problem is adding a link to create a new instance. For example, it would be this code for just SafetyTest:
<%= link_to new_safety_test_path %>
Now I want to be able to specify in my view, when I'm rendering _seasonal.html.erb, whether I want such a link for SafetyTest or TeamDue . I'm not sure what local to pass to this partial so that it creates the right new link without making a mess.
How should I go about doing this?

Take a look at Polymorphic URL helpers.

Related

Creating an action inside a controller, after it has been generated

I am working on a rails app, and have generated a Controller via
rails g controller Pics index upload
but now I would like to add another action
delete
do I do this by manually adding a delete method in the Pics controller?
Or do I need to run another generation. My concern is that by adding manually something may not get included (not sure what it would be, but something under the hood.)
Is this the best way of adding a new action to a generated controller?
If you add manually, just make sure you have the right route on your routes.rb.
Let's say you create your delete action inside your Pics controller.
def delete
# do stuff
end
On your routes.rb, you need to append the route to your resource like this, remembering to analyse if it is a resource that acts upon a member of your resource, or a collection. (More about this you can read on this guide http://guides.rubyonrails.org/routing.html#adding-more-restful-actions).
resource :pics do
collection do
post :delete
end
end
Or
resource :pics do
member do
post :delete
end
end
Remember that all RESTFUL actions are handled by default by the rails router, again, try to read the guide i showed earlier for precise information about the topic. Hope it helps.

Rails: $variable = HTML <form><input> type="text"

I have another question. I am sure that it is really really easy to answer to you but I just don´t know how to do it.
I am using Ruby on Rails 3.2.9.
In a view I want to define some variables which works fine like this:
<p>
<b>Artikelnummer:</b>
<%= #artikel.artikelnummer %>
</p>
<%= $aktArtNr = #artikel.artikelnummer %>
But now I want to save some User input (which should not cause a controller action or something else) in a variable - and I don´t know how to do it.
P.e. I want to save the "thing" that the user types into this form
<form name="Menge">
<input type="text">
</form>
in a variable called $aktMenge
It must be so easy but i´ve been trying this for a long time without success (but with getting a headache...)
P.S. after I just call a controller action which should do something with the variable...
Thanks a lot!
I'm struggling to see what you're trying to do. My suggestion would be to have a play around with Rails scaffolding so you can see how this works from the controller through to the views. e.g. open a terminal and try (maybe in a new project):
rails generate scaffold artikel name:string content:text ref_number:integer
rake db:migrate
Then go and look in your browser at "localhost:[port_no]/artikels" and you should be able to create, update and delete records through the web interface. Then go and have a look at the code generated to see if you can relate it to what you're doing.
Once again sorry for being new about all this RESTful stuff. I realized that due to the fact that i didn´t really know what exactly rails can do, i didn´t answer the right Question...
But this solved my problem:
Rails getting single values from request

Rails render partial (with action code)

Is there a way to render a partial view with accompanying code in rails?
For instance: I want to be able to create a partial view which will show the top 5 foobars on my site. This partial needs accompanying code to retrieve some foobars from the database, rank them according to an algorithm, and then output the view with the top 5.
I want to be able to include this partial on any page I fancy, preferably just by using something like
<%= render :action => "top_five_foobars" %>
Is this doable? I'm used to asp.net mvc where you can create an action that runs some code and returns a partial, but it seems like in rails it returns simply the template...
If google get you here, you might be looking for cells
Cells are view components for Rails. They are mini-controllers with
their own MVC stack, can invoke logic and render views. They bring
back OOP to Rails' view layer and make writing reusable portlets for
your applications fun.
You need something like a shopping cart, which appears on almost
every page of your app.
You wouldn't use a partial and a helper, would you?
It might not be the cleanest way, but what I did is that I created a helper method in the Application Controller that retrieves the top 5 foobars. Then I call this method in the views. I also cached the part of the view that shows the results.

Rails 3 - avoid duplication of controller code that generates a partial used in views generated by several other controllers

This is a noob question. My applets_controller has partials that will be called from views associated with other controllers.
(e.g. applets_controller _applet1.html.erb called from user_home_controller show.html.erb)
my link in show.html.erb is:
<%= render :partial => "applets/applet1" %>
I would prefer not to duplicate code to get variables from the applets_controller in all of the controllers of the views that will call the applet. How can i avoid doing this? I suppose i could transfer much of the code to the application_controller or to models. Is there a simpler solution?
Thanks.
I think you could put the relevant code & output into a Helper, and change your calls in the partial (_applet1.html.erb) to refer to that helper. That way, it would be available anywhere in your application. Depending on which helper you put it into, you may have to include a line to include AppletHelper or something similar.

Rspec2 and Rails 3 - View Spec Routing for Nested Resources

I have a nested resource, setup like this:
resources :chickens do
resources :eggs
end
The views for the EggsController are under views/eggs, but:
describe "eggs/index.html.erb" do
gives a "No route matches {:action => "create", :controller => "eggs"} error on calling render. How do I get RSpec to find the correct (nested) route for view specs?
As a side note, is there any way to specify a nested resource to the Rails 3 controller scaffold generator?
The test looks ok to me...
By any chance do you have a form on your eggs/index.html.erb for creating new eggs that might not yet be wired up correctly? It seems it may be trying to render the index view but failing because the view is trying build a route that doesn't exist? You'd want to make sure that the form is using the correct nested resource route. Does the view render when you load it up in the browser?