Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rails saving form data to custom model method and calling it in another view

ruby on rails form cookies rails-model mvc

  • Please log in to reply
2 replies to this topic

#1 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 12 November 2013 - 05:58 PM

i got a form in the index page where on submit it redirects to the show page and displays the relevant results. the form comprises mainly of select boxes and the form is also available in the show page.

i have saved the value selected in the select box in the index page, created a variable inside the show method of the controller which stores the params and called it in the select boxes default value in the show pages' form. so what a user selects in the index page, that selection is also displayed in the show page form.

<%= collection_select(:c, :id1, @categories, :id, :name, {:selected => @var}) %>

but if i go back and hit refresh in the index page, previously selected values are lost. obviously, the variable inside show method is unknown inside index method of the controller. i wont be able to re-use it.

so basically how am i suppose to store the selected values and show it anywhere.

i did something like this.

category controller

def index
@categories = Category.all
end

def show
@sel1 = params[:o][:id]
Category.safe(params[:o][:id])
end

category model

attr_accessible :name, :form
def safe(val)
self.form = val 
end

category index view

<%= collection_select(:o, :id, Origin.all, :id, :city, {:selected => @categories.form}) %>

it says undefined method form for #<Array:0x43d9478> where is the prob. i have defined it inside the category model and also added it to attr_accessor field.

how can i also save these params to cookies, so that on next visit, users see their previously selected value.



#2 Vell

Vell

    Dispatcher

  • Members
  • 45 posts
  • LocationWashington, DC

Posted 18 November 2013 - 09:21 PM

Is there a reason why you are using params instead of the data stored in your DB? If your saving the data to the database then that is the way it should be used. You probably could save the data in cookies but what if the user clears their cookies? To me this sounds like your relying a lot on the browser current state and cookies and that just may create more work for you in the process. I don't believe params is meant to be used the way that your using it give that any simple deviation and it causes results like what your seeing now. If your intent is not to save any data to the db then yes cookies MIGHT work, but I wouldn't advise doing things that way if you can help it.



#3 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 21 November 2013 - 02:58 PM

form data needs to be processed to display the result. that is done by params. i am trying to save the form data to use it in other views having the same form, for the current session and also to use it by default when the user returns.







Also tagged with one or more of these keywords: ruby on rails, form, cookies, rails-model, mvc

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users