Passing a list from a view to a controller? [closed] - ruby-on-rails

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm a relatively new Rails developer with a heavy Java/C# background and I'm trying to pass multiple time ranges from my view to my controller. For instance, the user could select the time range 9:00am to 11:00am as well as 2:00pm to 4:00pm. The first thing to came to mind was a list of times or a list of key value pairs so that I know when a time range starts and ends. I'm having trouble figuring out how to pass this information to my Rails controller though.
Is there an ideal Rails way of passing a list to a controller?

From Rails Guide http://guides.rubyonrails.org/action_controller_overview.html
request
GET /mytime?t[]=1&t[]=2&t[]=3
in view
<form method="GET" action="mytime">
<select multiple name="t[]">
<option value="1">1:00pm</option>
<option value="2">2:00pm</option>
<option value="3">3:00pm</option>
</select>
</form>
or
<form method="GET" action="mytime">
<input type="text" name="t[]">
<input type="hidden" name="t[]" value="11">
</form>
#in mytime_controller.rb
def index
params[:t] # return Array of values
end

You pass information from a view to a controller via http query string ('get') or form data ('post').
I would consider multi-select dropdown for this case.

Instead of dropdown i suggest you should create a cool for using some jquery plugin that will improve your application's user experience and yes create a rails form and submit it then you can have information in your controller.
Now is you are saving these selected time, that i think you should do then create a model if you haven't already and use form_for else your can use form_tag for your rails application.
Visit Here and find some time picker that suits you need.

Related

Razor view Html.ActionLink icon [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to add an icon near myLink in razor view,
in .html page i use ` Requête optimisée.
Now i have
#Html.ActionLink("Requete optimisée", "Index", "Requete")
Dont know where to add my icon .
I wouldn't use ActionLink and would do the following:
<a href="#Url.Action("Index","Requete")" class="my-link-class">Requete optimisée
<div class="my-icon"><img src="#Url.Content("~/icon.jpg")" alt="Icon Image" /></a>
More code but also more freedom and control over what is going on.
If you want show an icon just put a img tag before your link:
<img src="~/Content/Images/Image.png" />
#Html.ActionLink("Requete optimisée", "Index", "Requete")
And if you want to display image link then use this:
#Html.ActionImage("Requete optimisée", "Requete", "~/Content/Images/Image.png")

Retaining values even after form submit in ROR [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to create a simple calculator application .
calculator.html.erb
<input type=text name="operand_one" value="0"></td>
<input type=text name="operand_two" value="0"></td>
.
.
.
<input type=text name="output" value="<% =#result %>"></td>
I am able to get the output correctly but the values of the first two text boxes changes to 0 , I need to know how to retain these values ..
You're hardcoding the value to "0" with this part of the code
value="0"
you could change the lines to
<input type=text name="operand_one" value="#value_one"></td> #etc...
and in your controller
#value_one = 0 unless X
but X depends on how your controller works - do you go to a new action to solve the problem in the calculator? if so you'll have to use the params hash.
Not related to the question, but why are you using text for your calculation fields? and are you hard coding that html or it's just the output?

Dynamic rows and inputs

We have created a questionnaire application.
In that we are providing a user to enter one question and multiple answers and submit to store this questionnaire to DB.
For adding multiple answers for a single question, we need to dynamically add a row by clicking on add button and delete button for deleting the row on UI.
Now my question is:
How can I implement the feature of dynamically give a row to enter multiple answers?
Once multiple answers are added, how I will get multiple answers in the form field to add that in DB?
You can use an array to get all the different values.
E.g.
<input type="text" name="answer[]">
<input type="text" name="answer[]">
<input type="text" name="answer[]">
Regarding adding the extra rows:
what have you tried so far?
What do you have troubles with?
Do you have a specific question?

Creating a tag input box like on Stack Overflow using Ruby on Rails

How can I build a tag text box thing like here on Stack Overflow using Ruby on Rails?
Also, how do I handle the form post scenarios once the tags are submitted?
The Meta Stack Overflow question Can I use the tag textbox script? might help.
It points people towards the plug-in Chosen.
If you will take a look on the tags on Stack Overflow - it's just an anchor with span inside for removing it. And of course, you need to style it.
<a href="#">My Tag
<span>X</span>
</a>
As for posting the form: Use jQuery and an Ajax request to post it to the server. More information is in jQuery.ajax().
Source code for a demo is at http://jsfiddle.net/Fj3zx/6/.
Also check out the Choosen plugin for jQuery.

ASP.NET MVC - how to replace %20 with dash? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does StackOverflow generate its SEO-friendly URLs?
I made a simple form that contains the NAME and some other fields.
When user submits form, the following route will be called:
~/Profile/NAME
If user entered John Smith as NAME, my URL will be:
~/Profile/John%Smith
And I want it to be:
~/Profile/John-Smith
The application will display user details and again the same form at the top of the page.
NAME value will be passed to the form via ViewData.
I want to populate text box with John Smith, not John-Smith.
How can I do that? I ran out of ideas.
Thanks in advance!
EDIT:
I have found solution here: http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/.
It needs a little modification, but basically this is it.
HttpUtility, look into UrlDecode / UrlEncode
Edit
Use jQuery. Pseudo:
<input type="text" value="name" />
<input type="submit" id="submit" />
The jQuery
$('submit').click(function() {
$('name').val($('name').val().replace(' ', '-'));
});
It can look something like that, and when you want to display it, you just replace - with space, but remember, there are names that contain - and should not be replaced.
If it helps any, I used this from Jeff Atwood as well as the link you garnered above to make it happen.

Resources