Sorry th etitle of the topic isn’t very clear but it’s difficult to
summarize my problem!
I made a view without model but I display in table the content of a
variable named ‘@operations’. So each line corresponds to an operation.
In my table I have also a column with a button for each line. When i
click on the button I want to redirect to an existing page ‘Operation’
witch correspond to the operation of the line of the table like in the
index view with the ‘edit’ or ‘delete’ button.
But I can’t do the same think because I’m not working with a special
model in this view.
But I can’t do the same think because I’m not working with a special
model in this view.
You can, but you may need to do it a different way. The first
question is whether you need to use a form, do you need to post data,
or is each button effectively just a link?
I think so. You can use button_to do invoke the action, and pass the
operation number as a parameter or in the url. If you look at the
docs for button_to and google it to find more examples you will surely
find examples that meet your needs.
Colin
My matter is not how to pass the operation number to an other view but
is how can I recover this number. Indeed my button is not linked with
one operation…
An exemple of what I want to do is the edit button of Articles index’s
view, on click we are redirected to the edit view with only the article
of the line of the edit button clicked.
where n correspond to the line of the button and then I can reuse @operation in my other view because my two view are both using the same
controller.
I don’t know if I’m really clear?
I think so. You can use button_to do invoke the action, and pass the
operation number as a parameter or in the url. If you look at the
docs for button_to and google it to find more examples you will surely
find examples that meet your needs.
one operation…
An exemple of what I want to do is the edit button of Articles index’s
view, on click we are redirected to the edit view with only the article
of the line of the edit button clicked.
Just set the action on each button to the url that you want invoked,
so something like edit_article_path( article_ id ) where article_id is
the id of the article on that row.
Get it working just using link_to first, then make the link look like
a button if you want to. Note that since the link is not actually
performing an operation, but is just taking you to a different page,
then you want a link not a form button.
Just set the action on each button to the url that you want invoked,
so something like edit_article_path( article_ id ) where article_id is
the id of the article on that row.
Get it working just using link_to first, then make the link look like
a button if you want to. Note that since the link is not actually
performing an operation, but is just taking you to a different page,
then you want a link not a form button.
Colin
You misunderstood me, I can’t use this because in this view I haven’t a
model a use a varible in which I have all operations of my Operation
model!
So can’t use edit_article_path too!!
You said that, for example, the link should take you to the edit page
of an article. How does the user know what article that is?
Colin
No by exemple I understand the action of the button but it’s a totally
different think my button has to redirect to an Operation view but it is
not an edit view it’s a view created by me, and this operation has to be
the operation in the same line in the table with the button.
It’s difficult for me to explain in english I’am sorry !
not an edit view it’s a view created by me, and this operation has to be
the operation in the same line in the table with the button.
You don’t redirect to a view using a link or button, you request an
action in a controller, it is important to use the right words or we
will all be confused. It must be particularly difficult when one is
not working in one’s native language, I understand that.
So, when the button is clicked you need to request an action in a
controller, and from that you can then show whatever view you like.
So do you know what action you wish to invoke?
You misunderstood me, I can’t use this because in this view I haven’t a
model a use a varible in which I have all operations of my Operation
model!
So can’t use edit_article_path too!!
You said that, for example, the link should take you to the edit page
of an article. How does the user know what article that is?
My problem is not to redirect or to render and not to config routes.
My problem is the assignment of the variable @operation, how can I
assign the operation which correspond to the right button?
Operations
id_op1 name_op1 num_op1 button
id_op2 name_op2 num_op2 button
id_op3 name_op3 num_op3 button
To display this table I use a loop to display each element of each
operation contained in the variable ‘@operations’.
The when I click on the first button I wish to assign the first
operation in the variable ‘@operation’ annd then call a method in the
controller but it’s not my problem.
Be careful operation is not a model in this view but a variable. I don’t
know how to assign my variable in fonction of the button line?
id_op2 name_op2 num_op2 button
Be careful operation is not a model in this view but a variable. I don’t
know how to assign my variable in fonction of the button line?
That is not important. You cannot assign a value to a variable and
then pass it to an action, you must do it using the url of the link
to. The easiest way is to pass it as a parameter to the url, so the
url will look something like
htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to(“label”,
:controller=“operations”, :action=“some_action”, :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.
Note, however, that if in the operations controller all you want to do
is redirect to another action in order to edit an article, for
example, which is what I understood you wanted to do a few posts ago,
then don’t go to the operations controller at all, just construct the
correct link so that the button takes you straight to the activities
controller edit action so the edit page is displayed when the button
is clicked.
id_op2 name_op2 num_op2 button
Be careful operation is not a model in this view but a variable. I don’t
know how to assign my variable in fonction of the button line?
That is not important. You cannot assign a value to a variable and
then pass it to an action, you must do it using the url of the link
to. The easiest way is to pass it as a parameter to the url, so the
url will look something like
htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to(“label”,
:controller=“operations”, :action=“some_action”, :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.
Yes but my question was how can I have the value equal to the number of
the line of the table where the button is?
htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to(“label”,
:controller=“operations”, :action=“some_action”, :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.
Yes but my question was how can I have the value equal to the number of
the line of the table where the button is?
If you are dynamically generating the table, then you know what index
each row of that table is, don’t you? And if you are generating the
table using a collection of data, then you know the :id (for example, or
the color or stock number or any of the parameters of the underlying
object) of each item as you draw the table. Adding that value to the
link_to generated code is a trivial exercise.
htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to(“label”,
:controller=“operations”, :action=“some_action”, :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.
Yes but my question was how can I have the value equal to the number of
the line of the table where the button is?
If you are dynamically generating the table, then you know what index
each row of that table is, don’t you? And if you are generating the
table using a collection of data, then you know the :id (for example, or
the color or stock number or any of the parameters of the underlying
object) of each item as you draw the table. Adding that value to the
link_to generated code is a trivial exercise.
But the in the methode ajouter how can I have something like
@myvariable=n or @myvariable=@defautrec[i] where I is the current line
of the table?
<%= link_to controller: “pages”, action: “ajouter”, id: n %>
And then how can use the id in my methode?
But your code is otherwise so messed up that I can’t tell exactly what
you’re trying to do. What is the “do” at then end of the call to
link_to? Is the <%end%> after intended to be associated with
it? Or is it supposed to be for the @defautrec.each, but is just in the
wrong place?
I suspect you’re trying to wrap a button in a link, but that’s not how
HTML works, regardless of whether you’re using Rails or not. Do you just
want a button that acts like a link??? It that’s the case, you want
button_to:
Anyway, you cannot just stick “do” at the end of a function call in
order to start a loop. Remember, <%= %> just executes the Ruby code
inside and inserts the result into the HTML, and this is not legitimate
Ruby:
link_to controller: “pages”, action: “ajouter” do
Or, for more clarity, since this is the way it would be parsed: