how to write query string for webrick? - ruby-on-rails

My rails application requires few values to be specified in the text box.
My web page contains few text boxes .How can i specify the values of these text boxes in the url as query string while using webrick?can any one help, am new to this.
Thanks in advance.

If your url looks like this: localhost:3000/Accounts/1/edit?value1=A&value2=B and you want to put those in text boxes you must create some instance variables in the controller, and then reference them in the view.
Controller Action:
def edit
#value1 = params[:value1]
#value2 = params[:value2]
end
View:
<%= text_box_tag :value1, #value1 %>
<%= text_box_tag :value2, #value2 %>
If you followed my example, the first text box would display A and the second B.
Note that the webserver has no effect on this behavior. webrick, apache, mongrel, thin, etc... will all do this.

Related

How to store html for use in rails form_for?

I've got a page with
<p class = 'new-question'> Placeholder </p>
I have a form on the same page
<%= form_for(:question, url: question_path(Question.find_by(content:))) %>
I want the value of :content to be whatever the text of the <p> class is, ie in this case it should be Placeholder, I can't just put content: "Placeholder" because the text will be changed by a js script. How should I do this?
You can't use Ruby dynamically in the web page like that. question_path(Question.find_by(content: "whatever")) only resolves once, when the page loads, and just gives you a url that plops into the form.
The right way to do this is to dynamically change the url for the form directly with JS. If you only want to figure out the url based on the content/respective content, then you have two options:
Load all the Questions at once so that they're available to your JS on page load. Once the JS has the content, it can look through those Questions to find the appropriate url (where you'll also need to figure out how the routes are arranged--it's usually something simple like /questions/1/create.
If you're willing to make additional requests, then you can more sensibly use AJAX calls to ping your DB every time the content changes and let Rails tell you (through the AJAX) exactly what url to replace with.

Rails - File path to load content from a yaml file

I'm have a variety of text files with static long form text as content. Right now I am storing them in a separate "content" file in the config folder. For instance "../config/content/content1.yml" "../config/content/content2.yml" and so on.
I would like to string these files together in my application. So in my controller I have variables that attempt to pull the content of each file, for example
#content1 = YAML.load_file("#{Rails.root}/app/config/content/content1.yml")
#content2 = YAML.load_file("#{Rails.root}/app/config/content/content2.yml")
I then try load that variable into my view with
<%= #content1 %>
<%= #content2 %>
This and everything else I've tried doesn't seem to work though. I'd really just like to get the text to display in my view. Any help to point me in the right direction would be much appreciated. I'm very noobish with rails still.
I don't know why you don't want to store this data in DB, but lets describe what you need using only views.
According to comments that what you need are partials.
Example:
Lets have file app/views/content/editor1/paragraph1.html:
Example paragraph
Then in your view you can render this using:
<%= render partial: 'content/editor1/paragraph1' %>

Rails 3.0 . How to pass the select box value through link_to

I knew that Rails3 no longer supporting link_to_remote.... instead of that we can modify
the link_to method by using different syntax as follows :
link_to "Send",{:action =>"send_mail",:question_id =>question.id},:remote => true,:class =>"button small"
My problem is, in my view i keep the select box which contains the list of user's name near by send link button (which user the above syntax)... i want to pass the selection box value to link_to when user click the send button
Here is my View code :
"send_mail",:question_id =>question.id,:user_value
=>encodeURIComponent($F('user_id'))},:remote => true,:class =>"button small" %>
I don't know how to achieve this ....can any one please suggest me on this.
edit: ok now is see your last comment... never mind
What I got from the question, you are looking for something like this:
http://marcgrabanski.com/articles/jquery-select-list-values
It's not really a Rails problem since you can't change the Ruby code from within the rendered HTML (at least that would be very risky if it's possible). The code from above can be easily changed to your needs so that the user gets redirected to the URL that matches the button value.

Rendering field data as a link in Ruby on Rails

Ok, I think this is probably an easy question but for the life of my I can't figure it out. I have created a table called ugtags and in that table I have two columns (beyond the basics), 'name' and 'link'.
I am trying to allow a user to add a link to a page. Ideally they would enter the link title (name) and the url (link) and in the view it would display the title as a link to the url that was entered in the link column.
I there a way to do it by simply affecting the <%= link_to h(ugtag.name) %> code?
You should just be able to do:
<%= link_to h(ugtag.name), ugtag.link %>
See the documentation for all of the relevant options.

Keyword searching defaulting to AND

I successfully installed and set it up searchlogic for basic (keyword) searching. I have the following problem:
#search = Proposal.search(params[:search])
#proposals = #search.all
The above code works properly if I type in a keyword such as "red". It will bring up everything with red keyword. Or if I type in "green", it will bring up everything with green as a keyword. However, when I type "red green" in the search box it will ONLY bring up cases where the keywords are BOTH red and green (and not bring up instances where they may only have one of the two keywords). Yes, I am using keywords_like_any. I can see what the general problem is via debug, keywords_like_any: green red. The below code works as I want it to (bring up any instances of red OR green).
#search2 = Proposal.keywords_like_any("red", "green")
#test = #search2.all
I believe what I need to do to solve the issue is turn the first code to view params[:search] as an array? I tried doing params[:string].to_s.split (as shown in railscast) however it did not work.
The *_like_any is intended to be used with checkboxes form helpers (f.check_box) which outputs arrays into your params hash as opposed to f.text_field which outputs strings. If you still want to use them with a f.text_field you can :
split the field with Javascript on client side to output an Array
split it in your controller
Considering that your field is named keyboard here is some code that should solve your problem :
params[:search][:keywords_like_any] = params[:search][:keywords_like_any].split(' ')
#proposals = Proposal.search(params[:search])
You can skip the line #proposals = #search.all because search results works like an array.
You said you tried params[:string].to_s.split - I don't know if that's a typo, but it should be params[:search].to_s.split
I may not understand your question, but it appears to me that you're trying to implement something in the controller that belongs in the view. If your view has this:
- form_for #search do |f|
= f.text_field :color_like_any
then your initial controller example will work.

Resources