Something like ViewBag in Rails - ruby-on-rails-3

First of all I want to pardon all true rails developers, for asking is there original like copy.
My question is: Is there something like ViewBag in Rails, where I can put simple string?
Thanks.

My question is: Is there something like ViewBag in Rails, where I can put simple string?
No, thanks God, no. Rails didn't do the same mistake as Microsoft by allowing crap like ViewBag. You should use a view model if you want to pass data to a view. And by the way, you should use view models to pass data to view in ASP.NET MVC as well :-)
No, I am kidding, you could do this in Rails:
def foo
#name = "foo bar"
end
and in the view:
<%= #name %>
But please, don't use ViewBag in ASP.NET MVC. Use view models :-) This sentence makes me laugh. I am repeating it like a gazzilion times a day and I still see dudes using ViewBag. That's incredible.

Related

Creating a Rails partial that is model-agnostic

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.

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.

Understanding Rails helpers

I have two helper files. events_helper.rb and users_helper.rb.
Both of these helper files have a method called foobar. In events controller, index view. If I call foobar. Shouldn't it load the helper foobar thats in events_helper.rb?
Or is this not the way helpers work?
Seems like all helpers are available - so not sure which it would choose in your case, ideally the events controller one... and from your comment, it seems like its chosen the wrong one.
Could you give them distinct names?
Why are all Rails helpers available to all views, all the time? Is there a way to disable this?

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.