how to post :create from the default update url, rails 3 - ruby-on-rails-3

I'm sure this is pretty basic, but I'm somewhat new to rails and struggling to find a solution via search.
I'm implementing a message model to enable private messaging on a forum. I have the models resource nested within a users resource.
Currently the model works, but I want to enable a user to reply to a private message directly on the message show page. I.e users/1/messages/16 instead of users/1/messages/new. Currently this is the default route for 'update' within the MessagesController. Is there anyway to make the form on this page hit the 'create' action within the controller, instead of the 'update'?
Thanks.

Sure, I would try something like this:
On your show page just add a new form.
<%= form_for :message, :url => new_user_message_path do |f| %>
...
<% end %>
You can check the routes of your application using this command:
bundle exec rake routes
I suggest you to read the rails guide: http://guides.rubyonrails.org/

Related

simple_form - how to specify form's action attribute

I am using simple_form gem and have a ordinary rails scaffold which is working perfectly - I am able to update/create/destroy records.
When I add the views in application <%= yield %> a issue occurred - the simple_form gem is rendering a form with action "/" for the new action and I was not able to create new records.
In my controller, the path for create action is
#POST /webinars
So, I edit the action of the form from "/" to "/webinars" (using the browser console) and then successfully create a record.
I suppose that I should override the action of the from somehow, but was not able to find how.
Is this the real problem?
<%= simple_form_for #user, url: '/webinars' do %>
...
<% end %>

Destroy-like functionality using a custom controller method in Rails 3

I have a resource called patient_admissions that has all the RESTful routes. It is nested under another resource called patients. I want to add another method to my patient_admissions controller called discharge that updates a field in the model called :discharge_date (with Date.now) and saves that value in the table.
I would like this to work like the destroy method, in that if I have a bunch of patient_admission objects listed in a table in my index view, I could just click on the Discharge link and a confirmation box would appear, I would click 'ok' and then the value would be updated without having to first go to another view and deal with forms.
How can I do this without resorting to something like javascript? Many thanks!
In the rails guide on Routing, there's a section on adding additional restful actions:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
The example there would translate to something like:
resources :patient_admissions do
member do
put 'discharge'
end
end
This will recognize /patient_admissions/1/discharge with PUT, and route to the discharge action of PatientAdmissionsController.
This will at least allow you to get the routing set up for the action.
You could do this by using the link_to or button_to helpers in conjunction with a custom member route for your controller. Here is an example:
#routes.rb
resources :patient_adminssions do
put :discharge, :on => :member
end
Notice that I used PUT to add the custom route because the record will not be deleted, just modified. So according to the REST standards, I think put is the most appropriate.
# in your view
<%= button_to "Discharge", discharge_patient_admission_path(#patient_admission), :method => :put,
:confirm => "Are you sure you want to discharge this patient?" %>
This will create a button in a hidden form that when clicked will display the confirmation message and if it is confirmed then it will send a request to your controller action where you can set the appropriate discharge date like you suggested.

Rails app - Wanting something more than CRUD

I have a rails app where a User controller has all the CRUD methods new create destroy update however, I want an action now that does something more.
When the user clicks a button, I want to perform some logic and then forward them to their accounts page. The logic will go in the new method I make inside the controller. However, I cant find out how to make a form that submits to this new method I make in the User controller.
I'm trying something like this:
form_tag "/users"
but how do I make this form execute the new method I've made in the User controller
def some_logic
....
end
Update
After some reading, Is this the best way to go?
routes.rb
match '/download' => "users#some_logic"
view
= form_tag "/download"
This will execute the some_logic method
I tend to do in the view:
form_tag :action => :some_logic do
and in the routes you can have:
post "/download", :to => "users#some_logic"
That should make a form which sends the user off to the some_logic action within
UsersController
Take a look at the docs for further restful actions. You can find it here: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

rails3 authlogic-connect unknown attribute: oauth_provider

I'm trying to use the authlogic-connect plugin to add oauth support to my web app but I'm getting the error:
unknown attribute: oauth_provider
when UsersController#create is called. It is failing when I try to create a new user from the parameters that are being passed in:
#user = User.new(params[:user])
Sure enough, the parameters include this attribute:
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"lHX2BTFTd5xITTfY/X8A9R3vca3YaRuHFoWdqy8ZPik=",
"user"=>{"oauth_provider"=>"linkedin"},
"commit"=>"LinkIn"}
From the documentation, I assumed that this was what you are supposed to do:
Second, if you are using Oauth, you must include an input with name oauth_provider
and value twitter or whatever other provider you might want (see example apps for
dynamic example).
Any ideas on how this is supposed to work? All of the example forms are in yaml - which I don't know - and I'm trying to create this from using embedded ruby code. Here's the form that I'm using:
<!-- authlogic-connect hack -->
<%= form_for #user do |f| %>
<%= f.radio_button(:oauth_provider, "linkedin") %>
<%= f.label(:oauth_provider_linkedin, "Link In Fool") %>
<%= f.submit :value => "LinkIn" %>
<% end %>
As Dimitry pointed out, the best answer to this question is to use devise + omniauth instead of trying to get authlogic to work. Authlogic simply doesn't support rails3. I spent at least a week trying to get authlogic to work the way I wanted and I never solved the problem. I then spent a few hours getting devise + omniauth to do exactly what I want (i.e. you can create an account with LinkedIn or on the sight and you can associate them together so that you can login with LinkedIn or with email and password to the same account).

Rails login partial in different controller

Hey,
I'm pretty new to rails and for learning effect, I try to implement my own authorization system.
Right now I'm having a Page Controller to control some static pages and nothing more, and a Session Controller where I plan to implement most of the authorization process.
My problem is, I have no clue how to get my partial to use the sessions-controller, when I add it to one of the static pages controlled by the pages controller.
It stated out with this http://ruby.railstutorial.org/chapters/sign-in-sign-out#top but i don't want it on an extra page.
so I tried setting the routes and I got an exception "no path found for '/'" as soon as I deleted "resources :sessions" it worked fine again.
my partial looks like this:
<%= form_for(User.new) do |f| %>
<%= f.submit "Login" %>
<% end %>
there's also a div class="action" block around the submit but can't find out how to escape it
this is included into my home via
<%= render 'sessions/new' %>
Thanks for your help
edit my solution:
I added to routes.rb:
resources :sessions
Furthermore I changed form_for(#user) to
<%= form_for(:session, url => sessions_path)
so this works.
I Highly recommed that you look at the railscast http://railscasts.com/episodes/250-authentication-from-scratch , it will give you an idea how to create authentication without forgetting some important steps.
Then you can use the gem devise which is an excellent authentication gem.
Have you tried putting your functions and everything for authentication within a Session Helpers file? Then, in your Application Controller if you add "include SessionsHelper" this should give you access to all the helper functions from Session that you should need