I'd like to render multiline text in Rails, the action looks like:
def mutli_text
render :text => 'Word1\nWord2'
end
and I'd expect the response to be :
Word1
Word2
unfortunatly I get Word1\nWord2
Any help would be appreciated
(The action must render a multiline response to get the autocomplete jquery plugin working)
"Word1\nWord2"
You have to use double quotes to be able to use escaped characters.
But if you want to have that actually be a line break in the browser, you need to make it an actual html tag.
'Word1<br/>Word2'
Or even:
"Word1<br/>\nWord2"
you can actually do something like this:
(render :text => "line1\nline2").gsub("\n",'<br />')
It at least works on #render within a view (using HAML). I haven't tried it within a controller action.
Just render the text as plain. You can even change the content type to csv if you want to.
render :plain => 'Word1\nWord2', :content_type => "text/csv"
Related
I have in my controller something very straightforward:
def show
#group = Group.find(params[:id])
render :text => #group.inspect
end
I know that #group exists because if I send it to view (by commenting out the third line), it shows everything correctly. However, when I includethat render text line, I get simply a "#" and nothing else. What am I doing wrong?
It is rendering correctly if you view the source of the page. The problem is that the output is something like this:
#<Group id: 123, ...>
The browser expects HTML, so when it sees the opening bracket it thinks it's an HTML tag (but it's really not valid HTML).
Instead you could try escaping the HTML first:
render :text => CGI.escapeHTML(#group.inspect)
This will replace < with <, etc., properly displaying what you expect.
I'm new to Ruby and Rails. I just completed a course in Laravel, so I am aware of the MVC system(not a newbie as far as the basic concepts are concerned).
I have a rather simple question,
I am sending a POST request to my RAILS REST API,the body of the post request contains a json encoded string like this--->
Array ( [method] => POST [timeout] => 45 [redirection] => 5 [httpversion] => 1.0 [blocking] => 1 [headers] => Array ( ) [body] => {"post_content":"here is the post","post_title":"here we are ","post_author":"1"} [cookies] => Array ( ) )
As you can see,its coming from my php based blog.
My rails API is supposed to be taking the post content and automatically adding links to certains words, by comparing the words with some stuff that i have in an SQLite database.
Ok, so my problem is this:
I just want the response from the Rails controller, I dont want anything loaded into a view. The Rails Controller - returns the content, with 'a href' tags around words that are found in my database. This is to be sent back as the response to my post request, and i want to access it directly as the body of the response.
As of now I dont know how this is to be done. Laravel has the ability to 'return' whatever you want to , at the end of the Controller Action, but in Rails, everything seems to want to load into a view.
I have researched some questions here and found one which said 'render :nothing => true',but that renders nothing at all.Here is what my code looks like.
def process
content = params['post_content']
##perform db function and get back the content with the links embedded.
##HOW TO RETURN THIS CONTENT.
end
Personally, I think, i have to use the render_to_string method, but I have no idea how to do this.
Any help is appreciated.
Best Regards,
Richard Madson.
Some options to consider:
Render just the raw string as the http response body:
render :text => content
Render a view without the default surrounding layout:
render :layout => false
In that case your view could just be:
<%= #content %>
Or render the content as json:
render :json => { :content => content }
The question is, what do you want returned? Text? XML? JSON?
I'm going to assume you want JSON back based on the JSON going in.
respond_to do |format|
format.json { render json: #someobject }
end
It might be helpful to see the rest of the controller method.
If I understand correctly believe what you are looking for is
render :text => "response"
there is also - JSON, XML, nothing, js, file, etc - more information here http://guides.rubyonrails.org/layouts_and_rendering.html
How do I render a partial without having to supply the "_" in front of the file name? Is there a parameter I can call to not use it?
This problem popped up using RABL and Backbone - using RABL requires me to have a file in my views like "index.json.rabl". But, when I use embed the JSON right on the page load (as is usual with Backbone), I'm required to call the file "_index.json.rabl". These 2 files are the exact same thing, just required to have different names. I'm looking to use just 1 file, "index.json.rabl" and force the render() function to look for that file name, without the "_".
=> EDIT
The standard solutions that people have described below don't work. It's likely a RABL issue then? The below code always goes to the views/countries/_index.json.rabl file.
In my .erb file
countryList.reset(<%=get_json("countries", "index", #countries)%>);
In my application_helper.rb file
def get_json(view_path, view_action, object)
path = view_path + '/' + view_action + ".json"
return raw(render(path, object: object, :formats => [:rabl]))
end
You can render a file by doing the following:
render :file => "filename"
From the RailsCast #322 on RABL:
<div id="articles" data-articles="<%= render(template: "articles/index.json.rabl") %>" >
Start from here, and then figure out what's wrong. But it's clear that render template: path is the syntax you want.
did you try render :template => "file_name" ?
Ok try:
<%= render :file => 'views_directory/index' %>
Where views_directory is the name of your directory in the views 8)
OLD:
If the content is the same use:
render :partial => "index"
in index.json.rabl and the content in _index.json.rabl
I have a file in:
RAILS_ROOT/public/system/pages
It is a snippet of HTML. I would like to render it, along with other things, in one of my views.
But it seems like when I try to do a render from a view, Rails is always looking for a partial. But it doesn't pick up the file even when I name it with a leading underscore. How can I read and display this HTML snippet within a view?
have you tried with
<%= render :file => 'your/path/', :layout => false %>
inside the erb?
I want to render an HTML-EMail and send it to our customers using some ERB Templates.
The basic code I am using:
ERB.new("newsletter.html.erb").result(binding)
doesn't allow me to add partials to the html.erb-File. I would love to move the header and footer to a partial and use the render :partial-Method in that call.
Is this possible? What do I have to add?
This is what I came up with:
viewer = ActionView::Base.new(File.join(Rails::Configuration.new.view_path, "PATH/TO/PARTIALS"))
html = viewer.render(
:file => "PATH/TO/FILE.ERB),
:locals => {:variable => #var}
)
please correct me if there is a more elegant solution than this.