Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my view:
#foreach($questions as $q)
print question {{ $q->name }}
#foreach($answer as $a)
#if($a->question_id == $q->id)
print answer {{ $a->name }} //here I want only one answer
#endif
#endforeach
#endforeach
And this is my controller:
$questions = Question::get();
$answer = Answer::get();
return view('page', compact('questions', 'answer'));
I want to print questions, then only one answer per question. As there are many answers per question how can I limit it to print only one? Do I do it in controller somehow? I'm sorry I'm asking this a bit stupit question but I didn't know how to google it.
Assuming you've got relationships properly set up, it's as easy as this using Laravel's Collection class first method:
#foreach($questions as $q)
print question {{ $q->name }}
print answer {{ $q->answers->first()->name }}
#endforeach
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
str = "This website is <a href='www.google.com'>Google</a>, some other website is <a href='www.facebook.com'>Facebook</a>"
style_to_add = "style='text-decoration:none;'"
I want to add the style_to_add string to every hyperlink. So the result becomes
str = "This website is <a href='www.google.com' style='text-decoration:none;>Google</a>, some other website is <a href='www.facebook.com' style='text-decoration:none;>Facebook</a>"
This regex will work
(href='[^']+')
Regex Demo
More accurate regex will be to use
(\bhref='[>']+)>
Ruby Code
pattern = %r{(href='[^']+')}
str = "This website is <a href='www.google.com'>Google</a>, some other website is <a href='www.facebook.com'>Facebook</a>"
style_to_add = "style='text-decoration:none;'"
print str.gsub(pattern, '\1 ' + style_to_add)
Ideone Demo
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I know this error happens when I forget to wrap a sequence in parenthesis. But I was playing with this query for a while and put parenthesis where I saw fit. I still cannot get this query to work
<ul>
{for $R in doc("exp.xml")/exp/R
let $groups := tokenize($R/#an_attribute, '\s')
return (
<li>
<div>{$R}</div>
<ol>
for $group in $groups
return (<li>{$group}</li>)
</ol>
</li>
)
}
</ul>
you're missing the brackets to run the FLOWR inside the tag!
<ol>
{
for $group in $groups
return <li>{ $group }</li>
}
</ol>
and that's why you're getting the "variable has not been declared": it's not setting the for loop variable $group at all
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have a string like this:
src="http://www.google.com/calendar/embed?showTitle=0&mode=WEEK&height=600&wkst=1&bgcolor=%23FFFFFF&src=59flluvbaj110hp6ht5hrveof8%40group.calendar.google.com&color=%23B1365F&src=cnuvtn9nofljk5kq9381ic5odg%40group.calendar.google.com&color=%232952A3&ctz=America%2FNew_York" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"
and I want to extract the section in bold. It will always be between a src= and &. Currently, I'm doing
"sample string above".match(/;src.*?&/)[0][5, length-5]
but that seems really inelegant. Is there a better way to do this?
"sample string above"[/&src=(.*?)&/, 1]
The 1 means the first capture group
You don't need regex at all, just an understanding of what happened. The problem is that the content of src has been encoded for HTML entities, which, as a result, encoded embedded & between variables into &.
The fix is to decode the string first to reverse the encoding, then to split the string back into its components. You can do that using:
require 'cgi'
require 'uri'
uri = URI.parse(src)
hash = Hash[URI::decode_www_form(CGI::unescapeHTML(uri.query))]
hash['src'] # => "cnuvtn9nofljk5kq9381ic5odg#group.calendar.google.com"
An alternate to decode the query into a hash is:
hash = Hash[CGI::unescapeHTML(uri.query).split('&').map{ |q| q.split('=') }]
By splitting on &, then = we get an array of arrays, and can easily convert that back into a Hash, allowing easy access to any of the variables in the string.
While these seem like a longer path, they fix the problem and return values to their original form.
Normally we'd want it as a hash, but in this case we can't do all that because they have two "src" parameters in the query, causing the second to stomp on the first. If you want the first instead of the second, you'll need to grab it without converting to a Hash:
URI::decode_www_form(CGI::unescapeHTML(uri.query)).select{ |k,v| k == 'src' }
=> [["src", "*59flluvbaj110hp6ht5hrveof8#group.calendar.google.com*"], ["src", "cnuvtn9nofljk5kq9381ic5odg#group.calendar.google.com"]]
URI::decode_www_form(CGI::unescapeHTML(uri.query)).select{ |k,v| k == 'src' }[0]
=> ["src", "*59flluvbaj110hp6ht5hrveof8#group.calendar.google.com*"]
URI::decode_www_form(CGI::unescapeHTML(uri.query)).select{ |k,v| k == 'src' }[1]
=> ["src", "cnuvtn9nofljk5kq9381ic5odg#group.calendar.google.com"]
The string you display doesn't look correct though, instead it looks like something you cut and pasted from HTML. If so, you should use a parser to extract the content, not regex. And, in that case, here's how to do it right:
require 'nokogiri'
html = '<img src="http://www.google.com/calendar/embed?showTitle=0&mode=WEEK&height=600&wkst=1&bgcolor=%23FFFFFF&src=59flluvbaj110hp6ht5hrveof8%40group.calendar.google.com&color=%23B1365F&src=cnuvtn9nofljk5kq9381ic5odg%40group.calendar.google.com&color=%232952A3&ctz=America%2FNew_York" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no">'
doc = Nokogiri.HTML(html)
src = doc.at('img')['src']
=> "http://www.google.com/calendar/embed?showTitle=0&mode=WEEK&height=600&wkst=1&bgcolor=%23FFFFFF&src=59flluvbaj110hp6ht5hrveof8%40group.calendar.google.com&color=%23B1365F&src=cnuvtn9nofljk5kq9381ic5odg%40group.calendar.google.com&color=%232952A3&ctz=America%2FNew_York"
The Nokogiri at method doc.at('img') might change depending on the location in the document for the <img> tag, but dealing with that is a separate question.
You can use capture groups to do this as follows:
"sample string above".sub(/^.*src=(.*?)&.*$/, '\1')
Fix Your Quote Delimiters
Your string, as originally posted, has quoting issues. Make sure you escape your string properly. For example, you might use this alternate syntax:
src = %q{http://www.google.com/calendar/embed?showTitle=0&mode=WEEK&height=600&wkst=1&bgcolor=%23FFFFFF&src=59flluvbaj110hp6ht5hrveof8%40group.calendar.google.com&color=%23B1365F&src=cnuvtn9nofljk5kq9381ic5odg%40group.calendar.google.com&color=%232952A3&ctz=America%2FNew_York" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"}
Using Positive Lookbehind
You can use a positive lookbehind assertion to scan your string for all matches, and then use an appropriate Array method to access the one you're interested in. For example:
src.scan(/(?<=src=)[^&]+/).first
# => "59flluvbaj110hp6ht5hrveof8%40group.calendar.google.com"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
using nokogiri,
i want to find <p class="main"> Some text here...</p>
from an html document,
and then output the location as below or something that shows the tree
html > body > div class = "body" > p class= "main "
text="<html><body><div class='body'><p class='main'>some text here</p></div></body></html>"
doc = Nokogiri::HTML(text)
root = doc.root
node = doc.xpath('//p[#class="main"]').first
path = [node]
begin
node = node.parent
path.unshift node
end until node == root
path.map {|n| n.name}.join " > "
Exercise for you to add whichever attributes you want.