Home > Blockchain >  Update value without refreshing the pages in rails
Update value without refreshing the pages in rails

Time:07-28

I have to save users value without refreshing the pages.Till now I have created a save button but after hitting the button, page is refreshing. I need to remove the refresh.

This is my form:

<div >
   <%= form_for(:display , url: 'display',method: :put) do |f| %>
     <select name="display[display]" id="display_display">
     <option hidden><%= data['display']%></option>
     <option value="false">false</option>
     <option value="true">true</option></select>
     <%= f.hidden_field :id, value: field_id%>                   
     <button type="submit"  href="/">✔</button>
   <%end%>
</div>

This is roues for it:

put '/fields/display', to:'fields#display'

This is controller for it:

def display
    @field = Api::AnalyticsQueryBuilderMetadataService::Field.update_field_display(params.require(:display).permit(:display,:id))
end

This is my update_field_display method:

def self.update_field_display(params)
        id = params[:id]
        value = {'display' => params[:display] }
        response = Api::Connection.edit_request('fields',id, value)
end

This is my edit_request method:

def edit_request(type,id, params)
        response = connection.put("#{type}/#{id}", params)
        
      rescue Faraday::Error => e
        Rails.logger.error "Connection Failed #{e}"
        nil
end

This my connection request Call:

def connection
        Faraday.new(url: ENV['URL'],
                    params: { clientId: ENV['CLIENT_ID'] },
                    headers: { 'Authorization' => oauth_token_generation })
end

How to save the value on api call withour refreshing the page.

CodePudding user response:

You might want to render the same view in the controller update method as render does not redirect and keeps the current view intact

Code:

def update
 @user = User.find(params[:id])
 if @entity.save(entity_params)
   render :edit, notice: 'saved'
 else
  render :edit, alert: 'cant edit'
 end
end

CodePudding user response:

Without refreshing the page, you can use Action Cable. I think it is okie. https://guides.rubyonrails.org/action_cable_overview.html

  • Related