Options for running controller method from view - ruby-on-rails

Ruby on Rails 3.2: I made a table, a model and controller. I want to run the "new" method in my controller.
Off the top of my head I can think of a form to run the method. What other options are there to run controller methods?
In particular I would like to run the "new" method when viewing certain view files. Thank you

Well, you can add the new action to a link, use a form, and call the action using Ajax. Any other alternative other than those will violate MVC.

Related

Custom controller action or "CRUD" for gem method?

I am using the Socialization Gem for "Liking" and "Following" objects. I am doing this via AJAX. Right now, the only likeable thing I have is a Program.
I was able to get it working with both a Likes controller with create and destroy actions, as well as a custom controller action (like) in the Programs controller.
I can post the code if needed, but I was just curious if one of these is the better method, or if both are acceptable and it just depends on my needs. (The custom method programs#like is less code, but it seems less Rails-y.)

creating a simple html page in ruby

Following the ruby tutorial I am trying to create a simple html page.
I will need a controller, i think for some presentation manipoulation but not DB.
I thought that I would create a model, controller and view but then I see that
rails generate model mainMenu freeData1:string freeData2:string
creates the db script.
In order to achieve a simple managed html page that would not require db, what should I create ?
controler only? what is the best practice?
what methods should I put in it for it to be displayed?
thanks.
Yes, you'll just want a controller for the pages which will also create a views folder for your new controller that you can put HTML/ERB/whatever files in.
Since youre not working with the db, you can probably skip the need of a model.

how to refresh web page after executing stuffs in controller method on rails?

I'm developing project on rails 2.3.8 and I using drop down menu on one page and I observe it. In that it will go to controller method and I need to reload after executing things on that particular controller method. How can I do this on rails ?
Try putting at the end of your action in your controller redirect_to to the same path of the controller/ action.

Rails redirect without changing URL

I've some piece of ruby/rails code.
In one of the controllers say foo i have an action doo which does something and I want to redirect to another controller say bar and action say dar.
When I use redirect_to then URL in the address bar changes to /bar/dar while if I use render then I don't know how to render another controller's view.
I am using rails 2.3.5 so render_component is unavailable for use (which i found could be really really useful for me) -- so as a shortcut if you have any idea of alternate for render_component that will help me infinitely.
Any ideas?
[If am unclear please ask me details]
Instead of a real redirect, could you use an AJAX call to hit the action and pull in the appropriate views?
You can render the same partial from both views. You'll have a view for each action and in each of the views you'll have something like <%= render :partial => "partials/form" %>.
You can just call the action (like calling a function) and then render the template. But accordingly to the MVC pattern you should manage your controller-side logic on the controller and then loads the propper view.

Restful Rails Edit vs Update

I was trying to redirect to a different page after editing an entry, I assumed that it was using the update code because you are updating the database. It took me some time to realise that I was using the wrong action in the controller. Can someone please explain how edit and update work. Why are there two different actions? what are the differences between them?
edit action is responsible for rendering the view
update action is responsible for interacting with the model (db updates etc)
If you run rake routes you will see the difference between the verb and the action. Typically, the create/update actions are used when submitting a form. This differs from the new and edit actions as these are used to render the view (that displays the form to be submitted).
Another perspective - a bit redundant to highlight similarities and differences:
New is the precursor action to render a form, that upon submitting, runs the Create action.
(the view is typically redirected back to the index view showing a list of similar items you already created)
Edit is the precursor action to render a form, that upon submitting, runs the Update action.
(the view is typically redirected back to the index view showing a list of similar items you already created)

Resources