So recently I was helped out finding a way of using detect in rails to grab a url from text... however that would come very useful for me also in a controller if its possible.
My controller grabs some info and created a new entry into the DB based on incoming params, looks something like this;
#received_msg = Message.create(:content => params[:Text], :user_id => user.id, :status => 'new')
Now, I was hoping that using detect in my controller on the create action, I could search the :content for specific text or symbol and allow my users to define few things in content but I would assign that to a different column. I'm sure this isn't clear so let me show by example.
EXAMPLE:
Let's say my user send a message with content like so;
"Hey there, you should check this type of website out and make sure that its good to go"
Now, I'm assigning a USER_ID for the user that submitted this message, but the original owner could be someone else before the user submitted it. So I thought I would allow my user_id to enter #John into their message, meaning the original owner of the message was JOHN.. the new message would look something like this;
"Hey there, you should check this type of website out and make sure that its good to go #John"
Now in my controller when that message is created as a record, I'd like to take JOHN and assign it to the column username for example; I thought something like this would work but its giving me error .detect undefined..
#received_msg = Message.create(:content => params[:Text], :user_id => user.id, :status => 'new', :username => (params[:Text].detect {|original_sender| original_sender.start_with? "#"))
Maybe too many brackets?
you are missing } at the end
#received_msg = Message.create(:content => params[:Text], :user_id => user.id, :status => 'new', :username => (params[:Text].detect {|original_sender| original_sender.start_with? "#"}))
you can also use the below for what you want
#received_msg = Message.create(:content => params[:Text], :user_id => user.id, :status => 'new', :username => params[:Text].split.find{|w| w.start_with?("#")})
Related
I'm creating a customisable nav menu for our site and have run into the following problem.
I need to generate a URL to any controller and action on the site and optionally pass it parameters. I was able to do the former by simply saying:
url_for(:controller => nav[:controller_name], :action => nav[:action_name])
which is great for sending you to {controller}/{action}. eg. news/articles
Throwing options in suddenly changes the game. Now I need to send you to something like:
{controller}/{action}/{category}/{slug}/{id}
eg. news/articles/world-domination/montana-max-vows-revenge/12345
the helper for the above would be something along the lines of:
news_article_path('world-domination', 'montana-max-vows-revenge', '12345')
and I haven't been able to replicate that in a vanilla url_for due to the arguments.
What I have done, and I don't really like is:
url_for(send("#{nav[:controller_name]}_#{nav[:action_name]}_path", *nav[:options]))
which generates the helper using send and then passes it a kwargs list. I'm sure there's a better way to do that surely?
You can do this cleanly if you are able to name the options (split here over lines for legibility):
url_for({
:controller => nav[:controller_name],
:action => nav[:action_name]
}.merge(nav[:options] || {}))
where
nav = {
:controller_name => 'news',
:action_name => 'articles',
:options => {
:category => 'world-domination',
:slug => 'montana-max-vows-revenge',
:id => '12345'
}
}
I have my posts generating a :key_code when created. I want a user to be able to use that code in a form field to destroy the post they made, without being signed in. My difficulty is with form_tag... how could I make a form_tag view to check the :key_code entered with the one stored in the database? I'm having a hard time understanding form_tag.
My route looks like this:
get 'posts/:id/code' => 'posts#code', :as => :code
Any help is much appreciated
If I understand correctly then your route is wrong as you need a dynamic param code, so you should use :code instead of simply code:
get 'posts/:id/:code' => 'posts#code', :as => :code
To generate a url to that route in form_tag you use:
form_tag code_path(#post, :code => #post.key_code)
Hi I have read all the other post relating to this but I think I am missing something fundamental. I am using mini_fb in my ruby on rails application for handling the facebook api. I have the following code:
current_user.session.post('me', :type => :feed, :params => {:name => "name",
:to => "{\"data\":[{\"name\":\"#{friend.facebook_name}\",\"id\":\"#{friend.facebook_id}\"}]}",
:link => url_of_app,
:description => "desc",
:actions => "{\"name\": \"action name\", \"link\": \"url\"}"})
The above posts to the current user's wall with or without the "to" parameter set. Everything works, except for the "to" parameter. I have read the graph post to wall over and over again and I can't figure out what is wrong with this code of mine. I would really appreciate it if someone could point out my mistake.
I've never used ruby's version, but probably the problem is in the first parameter. You are targeting 'me' feed, while should be targeting your friends feed. Try fetching your friend id and doing something like
current_user.session.post(friend.facebook_id, :type => :feed, :params => ...)
Wow, mini_fb looks so verbose :)
Telémako is right, you need to use your friends id. I give you another alternative for more nice code. Use Koala.
https://github.com/arsduo/koala/wiki/Graph-API
#graph.put_wall_post("explodingdog!", {:name => "i love loving you", :link => "http://www.explodingdog.com/title/ilovelovingyou.html"}, "tmiley")
=> {"id"=>"83901496_520908898070"}
I use it in my projects and works very well.
I created the following route:
map.todo "todo/today",
:controller => "todo",
:action => "show_date"
Originally, the 'show_date' action and associated view would display all the activities associated for that day for all the Campaigns.
This ended up being very slow on the database...it would generate roughly 30 records but was still slow.
So, I'm thinking of creating a partial that would first list the campaigns separately.
If someone clicked on a link associated with campaign_id = 1, I want it to go to the following route:
todo/today/campaign/1
Then I would like to know how to know that the '1' is the campaign_id in the controller and then just do its thing.
The reason I want a distinct URL is so that I can cache this list. I have to keep going back to this and it's slow.
NOTE: It's possibly the problem actually is that I've written the queries in a slow way and sqlite isn't representative of how it will be in production, in which case this work-around is unnecessary, but right now, I need a way to get back to the whole list quickly.
The code above by #Damien is correct but incomplete. It should be:
map.todo "todo/today/campaign/:id", :controller => "todo", :action => "show_date"
in your views all you have to do is:
<%= link_to "Campaign 1", todo_path(:id => 1) %>
or simply
<%= link_to "Campaign 1", todo_path(1) %>
and the particular campaign id can be fetched using params[:id] in your action.
And yeah, sqlite is not production ready.
EDIT: The latter part is quite easy to implement:
However, you have to change the route slightly,
The route will now become,
map.todo "todo/today/:campaign/:id", :controller => "todo", :action => "show_date"
in your views:
<%= link_to "Campaign 1", todo_path(:campaign => "campaign", :id => 1) %>
In your todo controller, show_date action:
def show_date
#IF YOU ARE USING THIS REPEATEDLY IN LOTS OF DIFFERENT ACTIONS THEN A BETTER PLACE FOR THIS WOULD BE AS A HELPER IN application_controller.rb
if params[:id].nil? && params[:campaign].nil?
#DO SOMETHING WHEN BOTH ARE NIL,
elsif params[:campaign]!="campaign"
#DO SOMETHING WITH CAMPAIGN BEING SOMETHING OTHER THAN "CAMPAIGN"
elsif params[:campain]=="campaign" && params[:id].nil?
#DO SOMETHING WITH ID BEING NIL.
else
#FIND YOUR CAMPAIGN HERE.
end
end
Hope this helps. :)
Just with the following :
map.todo "todo/today/:id",
:controller => "todo",
:action => "show_date"
This will create the /todo/today/:id url where id is whatever you set in the url.
You can then access it in your controller with params[:id].
You might be interested in reading Rails Routing from the Outside In, particularly the section about resources.
I am building an application and it needs to have a feature whereby one user can send another user a message. The system can be the most basic type available in rails and that would suit me fine.
Any ideas on how to go about this?
Thanks.
Table structure like this:
Users
name,pwd
Messages
title,body
UserMessages
user_id,message_id
Why don't you use acts_as_messageable plugin:http://www.philsergi.com/2007/10/actsasmessageable-plugin-released_04.html ?
Similarly, there are other plug-ins for authentication (restful authentication).
So i have implemented the DB tables and now need to pass the data around my system which im finding quite troubling. When the user clicks "send message to" on my form i need it to carry the id of the profile which the user is viewing. I thought this would do that:
<%= link_to "Message", :action => 'message', :id => #user.id %>
Now this pass the persons ID who i was looking to the message action (i know #user.id should work because i use #user.detail to view other details about the user on that page)
My controller should then receive that #user.id, heres my controller:
def message
#reciever = User.find_by_id(params[:id])
end
and in my view for i want to show the recievers id so i thought that
<label>Send Message To: <%= render :text => #reciever.id %></label>
would be suffiencent.
Any ideas?