How to get the id of the model I am currently viewing - ruby-on-rails

So lets say I am in /post/2. Which shows me the post with the ID of 2.
Now in controller, how can I see what the current post id is that is being viewed?
My case is a bit different because I am also rendering a partial from another controller into the posts page. And if I delete one of the entries from that partial, I will also need to know the current post id that is being watched.

I'm not sure I completely understand your question, but you can access the ID of any model by simply calling the id method on it. For example if you have a model called Post and an instance of it called #post you would get the id by calling #post.id.

You can get the ID of a model by simply calling id on it. Example: #post.id
If you didn't fetch the model from the database yet, you can get the ID parameter from the URL by accessing params[:id]

Related

Rails controller initiate others controller create action

I need to make new Footprint on every action user makes in database. How can I initiate Footprint.new() on other controller also giving automatic statement with it.
For example when User deletes entity.
entity_controller action delete will pass such statement as "user"+ "{ #user.name }" + "deleted entity "{ #entity.name }" to footprint.description.
Also it should pass current user id to footprint.user_id
Don't know how to do it!
I was not clear on your question , but what i felt was that you want to delete an entity and then wanna pass deleted entity name to another action which is , in some other controller .
def delete
entity = Entity.find(params[:id]
entity_name = entity.name
entity.destroy!
footprint_path(entity_name)
end
you can use path helpers when you have defined resources in your routes.rb , which in this case is , resource "footprint"
Update
To track user's activity you can use paper_trail or Userstamping
I guess you need a user activity feed on your application. You can make use of the Public Activity gem for achieving this functionality. There is also a nice Railscast about the gem.

Getting params from URL in model's create method

Is there a way to get the params from URL in the create method?
My URL for the "new" view is:
/model_name/new?other_model_id=100
I would like to be able to alter the model with ID 100 when I create a new model. I've tried calling params[:other_model_id] in my "create" method, which returned nil and I tried setting the following variable in my "new" method:
#other_model = Model.find(params[:other_model_id])
I have a field called "other_versions" in my model, which is an array of model IDs. When I create a new model I want to add the new model's ID to the array of model IDs in the old model.
Why don't you use the after_createfilter (http://guides.rubyonrails.org/active_record_callbacks.html) and just add the "other_model" id on an hidden field on your create/edit form?
Please make sure, if you are using Rails >4, to add that parameter on your strong parameters (http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html) otherwise it will always be empty when you check on params.

How to code this in Rails?

I have three models and their corresponding tables and controllers:
Request
DirectPatch
UTPFiberPatch
The user creates a new request and defines the type of request: Direct or UTP/Fiber
Once the user hits save, the Request will be saved and the user will be redirected to an edit screen to create all the patch entries corresponding to that request.
Each patch entry will be saved as a single row in either the Direct or UTP table depending on the type of request selected. A column called request_id will act as the foreign key.
The view and form will be different for both Direct and UTP/Fiber.
The user can view all requests on a single homepage and click to edit. The user can then see all the patch rows for that request on a single page and click to edit existing or add new.
What would be the best way to set up the routing, controllers and
views for this?
How does the Request ID get passed when creating new patches and automatically saved?
I'm not sure exactly what the technical name is for the problem I'm having if there is one, but I'm happy to add more detail and answer questions if needed.
You could use a polymorphic association on the request model:
belongs_to :patch, :polymorphic => true
Add to your Request table two columns:
patch_id : integer
patch_type : string
When you create a request and the user selects the patch type, you assign this patch object to your request, which will populate the two new columns. From you request model you can then call
request.patch
Which will give you back the appropriate type of patch based on the details stored in the database (note - you can't do eager loading with polymorphic associations).
To do the views you can render a partial in the edit screen, based upon which type of patch it is.

Instance variable in rails - apart from views where can we use it and for how long is it available

I have created a instance variable in rails project, which gets its value from a url parameter like example.com/value. This variable is created in new action, now can it also be used in create action, of the same model.
The value is a id of another model altogether and both the models are associated, I need to create the instance variable in former model.
I need to know, for how long the instance variable is available, and can be use the instance variable of one model in another model.
Clarification with real example
Supposingly there are two models, one is User model and other is Referral model. The root is root :to => 'users#new. Now the user will coming here via example.com/value, where value is the id for Referral model. Now using this value I have to increment two fields: One is visits, which shows how many visits did that particular url bring. Other is signup, which will increment if there is a signup using that value.
I have passed this value via routes in users#new, which I use to increment the visits column of Referral model. Now if the users signup, the users#create would be executed, and I want to be able to use the value in the create action as well, to increment the signup column in Referral model.
As of now, I understand that the instance variable I created in new action to store the value cannot be used in create action. Now how can I achieve this.
In general instance variables only last as long as the user's HTTP request, so they can not be created in one action and used in another.
You could try storing the variable in the session, a hidden input field on the HTML form generated by the new action, or in the urls of links generated by the new action.
I don't know exactly what you are doing, but from the names of your two actions it sounds like there is probably an HTML form involved, so I think the best thing is to use a hidden input, something like this:
<input type="hidden" name="model_id" value="<%= #model_id %>" />
Instance variables only last for that call and in the class they are defined, with the exception of the views. If you have a controller with two methods where one method is your route and another is used internally, then it will be available to both, it is also available to your views.
e.g.
test_controller.rb
def index
something_else
p #variable #outputs "foo" in the terminal
end
def something_else
#variable = "foo"
end
However it would not be available between create and new as these would be called in different requests.

How do I pass a parameter to a form in Rails that persists on validation error?

I'm attempting to create a hierarchal menu system in rails.
My menu model is in a tree data structure, and I need to pass the parent information to the new action. The best way i found to do this was to pass the parent's id as a querystring. This works, unless validation blocks creation. The problem is that after the new view is rendered, the parameter is no longer present in the url.
Am i going the wrong way with this? Should I just be persisting the value in the session? Should i create a dropdown to pick the parent item (I'd prefer to just let the user click "Add new item here"). This must be a common issue, but I havent been able to find antything relevant.
Update: To clarify a question, I tried doing the following to pass the parameter when the view is rendered in the failure case, but the url has already been set at that point (we've already posted to the controller's base url).
format.html { render :action => "new", :parent_id=> params[:parent_id] }
I need another way to pass the parameter.
I figured it out. I just added a hidden field to hold the parent_id. When the browser requests the new form, i put the parent_id into the model object, and it's persisted on psotback.
Not 100% clear on what you are trying to achieve.
After the operation fails validation, you render the view again?
Can you not just pass the parent_id to this view as a parameter?

Resources