Getting url params into controller - ruby-on-rails

On my website I load a _form made by rails scaffold onto my page, which is under my home_controller.
It only has one text input box for name, but the table im inputting this entry into also requires a bigint value, called course_id.
I pass this course_id value into that page by passing it as a url param.
then in my mod_controller i have #mod.course_id = Course.find(params[:course_id]).id
in my def create function.
any suggestions to help this would be appreciated.

Maybe you want to do this in your Model instead of your Controller? (I am not sure I understand your question though)

Related

Rails - Passing Parameters through link_to

I want to create an Add to Watchlist column + link after the description field ,so the selected movie to be stored on another rb. page from the admin section called "Watchlist".
The concept is similar to a Shopping Cart,passing the data from one page to another.
How's that possible in ActiveAdmin?
I would appreciate your hints and advices.
I'm not familiar with ActiveAdmin, but if your question is how to pass parameters through link_to, try this:
link_to 'Somewhere', some_path(param_1: 'foo', param_2: 'bar')
When you click through, the params should be present in the query string and in the params hash that Rails provides. You can pull out the values with:
params[:param_1]
params[:param_2]

rails passing parameter from view to a different controller as a string

Im trying to pass a parameter (a file path : eg. user/lauren/tools/) from a view in RAILS to another different controller using RUBY. I want to use the file path (as a string) to be matched with a regular expression. To do that, right now Im using the params [:parameter] method as follows in the controller action where Im setting the variable instance:
#variable = /^.+\/[^\/]+$/.match(params[:name]).to_s ---where :name is the parameter passed from view
Right now, I dont get any output when I try to display that in the corresponding view of that controller....im guessing its the params [:name] method I need to replace or modify?
Id really appreciate views on this...thanks!
While your server is running open a new terminal window and type rails console. This will let you see what your variables actually contain. You can type params and see what the hash has in it and see why your regex is failing. It sounds to me like you are trying to match your path, not your params.

Handling forms in rails

I am a little confused with forms in RoR.
I have a contacts page with a corresponding method in my controller. What I am trying to do is have a form so people can leave a message. I will then be emailing that message to myself.
I have the form and everything created. However, I am a little confused on how I would actually get the data from the form once they hit the submit button. would it just be accessible through my contacts method in my controller using params[:message]?
Also, what if I had multiple forms on one page? Would I just be doing params[:message1], params[:message2], etc., in the contacts method in my controller?
Take a look at this answer for details on what exactly the params hash is. When you reference params[:message], this implies that you are POST'ing to your controller action with, say, "message[subject]=abc123", which Rails helpfully turns into a hash with a key like: params[:message]['subject'].
If you're looking to send an email, check out mail_form, which simplifies creating a non-database-backed model that get's turned into an email.
Lastly, about having multiple forms on a page: each form POST's to its action and includes all of the form elements that are children of that form in the DOM. So, only the children of that message will actually be included in the params[:message] hash. You can use fields_for to have multiple models within a single form.

Ruby on Rails: How do I pass a variable along with f.submit?

Ruby on Rails: How do I pass a variable along with f.submit?
no you can't pass along with submit button.
but you get name of button as params[:commit] in controller
The parameters you'rer passing would be the data in the form, yes?
What parameter/data are you trying to receive? If you want to know the name of the user who performed the submit, or the current time, or something similar, there are other ways to do it.
As Salil mentioned, the parameters/form data are accessible in the controller via the "params" array.

Creating a specialised view filtering form in Rails

G'day guys, I have a current set of data, and I generate multiple analyses of this data (each analysis into its own active record item called a pricing_interval) using a helper function at the moment.
Currently to analyse the set of data, you need a start time(using datetime_select) an integer (using text_field) and a name (using text_field)
I would like on submission of the form to be redirected to the index page of my pricing_interval, as the values will be re-generated. Manually generating a range proves that my helper methods work.
How would I build a form that on submit would send parameters to a function in the form of (date,integer,name) so that it could immediately begin work whilst redirecting the user to server/pricing_intervals
Anything at all would help, I've spent hours over the past few days trying to get the rails form syntax working properly to no avail, a really straightforward guide to what I would implement to get this working would be amazingly appreciated.
I've looked through the form guides, as I'm not creating an object, but merely parsing params, there's got to be an easy way to do this, right?
You can use the rails form helper hidden_field_tag to put in whatever form fields you want. They are added to the params hash that your controller will see. You don't have to only send form fields that correspond to an ActiveRecord model.

Resources