Hi everybody, in my view (operation_en_cours) I display the content of a
variable (myvariable) and I have a button linked to a method ( for
exemple change)defined in my controller in which I change the content of
my varible like this:
@myvariable="hello"
def change
@myvariable="test"
respond_to do |format|
format.html { redirect_to pages_operation_en_cours_url }
format.json { head :no_content }
end
But when I click on the button my fonction is called y saw it in the url
(http://192.168.152.196:3000/pages/change) but then in the view I always
have "hello". I think the is not refresh but I don't no how can I do?
Indeed if I define myvarible like this @myvariable=Article.all and then
def change
@myvariable[0].name="test"
@myvariable[0].save
respond_to do |format|
format.html { redirect_to pages_operation_en_cours_url }
format.json { head :no_content }
end
and in my view @myvarible[0].name then when I call the method the view
is refresh and the name is change.
How can I do the "equivalent" of save for my variable?
The problem is the redirect. When you do a redirect, Rails actually responds with a code 301 to the browser and redirects the browser to the new url. When Rails receives the redirected request, it’s treated as a completely new request, so no instance variables will persist.
The only way to do what you want is to use a render instead of a redirect.
The only way to do what you want is to use a render instead of a
redirect.
Thank you very much for you answer and your explanation, I'm in business
using:
def ajouter
@reperecarte[0]="test"
respond_to do |format|
format.html { render :template => "pages/operation_en_cours" }
format.json { head :no_content }
end
end
Probably, but I can’t really tell without having more knowledge of the underlying controller and views. If you have a button that links to the change action, it’s going to create a response from that action and whatever routing is associated with that action will be in the url of that action unless you have told the system to do otherwise. The one thing that is commonly misunderstood with rendering is that execution remains within the action that called the render, in this case, the action change. Any instance variables that are referenced in the view must be defined within that action. Even the following:
render action: “operation_en_cours”
doesn’t really execute the action. It renders a view using the template associated with the action operation_en_cours. Any information required by the template from the action (such as instance variables) will need to be defined in the action that is calling it (in this case, change).
instance variables that are referenced in the view must be defined
within
that action.
All my variables used in my view are defined in my controller in a
methode inivariable and then I use before_filter :inivariable then they
are initialized.
Even the following:
render action: "operation_en_cours"
doesn't really execute the action. It renders a view using the template
associated with the action operation_en_cours. Any information required
by
the template from the action (such as instance variables) will need to
be
defined in the action that is calling it (in this case, change).
Okay I see the difference now but then is it possible to call an other
mathode after one is already call? Is the only way to report the second
methode in the statement of the first one?
Not sure I understand the question, but I would recommend a primer on ruby. You can call one method from another, but I’m not sure it’s going to resolve your issue and I really don’t have enough information to suggest how I would go about it.