Update views after change variables

56 views
Skip to first unread message

Fab Forestier

unread,
May 21, 2014, 7:42:39 AM5/21/14
to rubyonra...@googlegroups.com
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?

Fab

--
Posted via http://www.ruby-forum.com/.

mike2r

unread,
May 21, 2014, 8:19:36 AM5/21/14
to rubyonra...@googlegroups.com
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.

Fab Forestier

unread,
May 21, 2014, 8:36:56 AM5/21/14
to rubyonra...@googlegroups.com
mike2r wrote in post #1146701:
>
> 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

Fab Forestier

unread,
May 26, 2014, 9:42:19 AM5/26/14
to rubyonra...@googlegroups.com
mike2r wrote in post #1146701:
> On Wednesday, May 21, 2014 7:42:39 AM UTC-4, Ruby-Forum.com User wrote:
>> format.json { head :no_content }
>> respond_to do |format|
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>
> 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.

There is an issue with your solution too because the view is updated but
in the url of the page we can see the name of the function and then we
can't call other other function by clicking on other buttons of the
view.

Did I forgot something in my code?

mike2r

unread,
May 26, 2014, 11:20:43 AM5/26/14
to rubyonra...@googlegroups.com
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).  

Fab Forestier

unread,
May 27, 2014, 3:08:18 AM5/27/14
to rubyonra...@googlegroups.com
mike2r wrote in post #1147137:
. Any
> 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?

mike2r

unread,
May 27, 2014, 7:17:56 PM5/27/14
to rubyonra...@googlegroups.com
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.  
Reply all
Reply to author
Forward
0 new messages